使用Spring MVC HandlerInterceptorAdapter从HttpServletResponse记录响应主体(HTML) [英] Logging response body (HTML) from HttpServletResponse using Spring MVC HandlerInterceptorAdapter

查看:1558
本文介绍了使用Spring MVC HandlerInterceptorAdapter从HttpServletResponse记录响应主体(HTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试记录(仅为了简单起见,现在是控制台写入)最终呈现的HTML,它将由HttpServletResponse返回。 (即正文)为此,我正在使用Spring MVC中的HandlerInterceptorAdapter,如下所示:

I am trying to log (just to console write now for simplicity sake) the final rendered HTML that will be returned by the HttpServletResponse. (i.e. the body) To this end, I am using the HandlerInterceptorAdapter from Spring MVC like so:

public class VxmlResponseInterceptor extends HandlerInterceptorAdapter {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println(response.toString());
    }
}

这按预期工作,我看到HTTP响应头在控制台中。我的问题是,是否有一种相对简单的方法可以将整个响应体(即最终呈现的HTML)记录到控制台,而无需使用PrintWriters,OutputStream等进行跳跃式插孔。

This works as expected and I see the HTTP response headers in the console. My question is if there is a relatively simple way to log the entire response body (i.e. final rendered HTML) to the console without having to resort to doing jumping jacks with PrintWriters, OutputStream's and the like.

提前致谢。

推荐答案

使用Servlet 过滤 而不是一个春天 HandlerInterceptor ,因为允许过滤器替换请求和/或响应对象,你可以使用此机制用记录响应输出的包装器替换响应。

This would be better done using a Servlet Filter rather than a Spring HandlerInterceptor, for the reason that a Filter is allowed to substitute the request and/or response objects, and you could use this mechanism to substitute the response with a wrapper which logs the response output.

这将涉及编写 HttpServletResponseWrapper ,覆盖 getOutputStream (和也可能 getWriter())。这些方法将返回 OutputStream / PrintWriter 将响应流吸入日志的实现,此外还发送给它原始目的地。一个简单的方法是使用 <来自 Apache Commons IO 的code> TeeOutputStream ,但是实现自己并不难。

This would involve writing a subclass of HttpServletResponseWrapper, overriding getOutputStream (and possibly also getWriter()). These methods would return OutputStream/PrintWriter implementations that siphon off the response stream into a log, in addition to sending to its original destination. An easy way to do this is using TeeOutputStream from Apache Commons IO, but it's not hard to implement yourself.

这是一个你可以做的事情的例子,利用Spring的 GenericFilterBean DelegatingServletResponseStream ,以及 TeeOutputStream ,以简化操作:

Here's an example of the sort of thing you could do, making use of Spring's GenericFilterBean and DelegatingServletResponseStream, as well as TeeOutputStream, to make things easier:

public class ResponseLoggingFilter extends GenericFilterBean {

   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
      HttpServletResponse responseWrapper = loggingResponseWrapper((HttpServletResponse) response);     
      filterChain.doFilter(request, responseWrapper);
   }

   private HttpServletResponse loggingResponseWrapper(HttpServletResponse response) {
      return new HttpServletResponseWrapper(response) {
         @Override
         public ServletOutputStream getOutputStream() throws IOException {
            return new DelegatingServletOutputStream(
               new TeeOutputStream(super.getOutputStream(), loggingOutputStream())
            );
         }
      };
   }

   private OutputStream loggingOutputStream() {
      return System.out;
   }
}

这会将所有内容记录到STDOUT。如果你想登录一个文件,它会变得更复杂,确保流关闭等等,但原则保持不变。

This logs everything to STDOUT. If you want to log to a file, it'll get a big more complex, what with making sure the streams get closed and so on, but the principle remains the same.

这篇关于使用Spring MVC HandlerInterceptorAdapter从HttpServletResponse记录响应主体(HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆