如何在拦截器中读取httpServletResponse?

时间:2017-08-25 10:38:48

标签: spring spring-mvc spring-boot

我有一个春季启动应用程序。现在我需要在拦截器中读取请求和响应。我使用HttpServletRequestWrapper替换DispatcherServlet中的请求

@Component("dispatcherServlet")
public class FofDisPatcherServlet extends DispatcherServlet {
    @Override
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultiReadHttpServletRequest requestWrapper = null;
        try {
            requestWrapper = new MultiReadHttpServletRequest(request);
            super.doDispatch(requestWrapper, response);
        } catch (Exception e) {
            super.doDispatch(request,response);
        }
    }
}

在我的拦截器中,我可以阅读请求正文。但是当我想阅读响应主体时,它不起作用。当我替换CustomerDispatcherServlet中的响应时,我没有得到任何响应。我尝试了ContentCachingResponseWrapper,但我得到了有效载荷""

这是一个老问题。我已经搜索了一些问题,但没有找到合适的解决方案。

我知道我可以用AOP来解决问题。但我想知道如何在拦截器中解决这个问题?

这是我的拦截器代码

public class CustomerInterceptor extends HandlerInterceptorAdapter{
    @Override
    public void postHandle(...){
        MultiReadHttpServletRequest req = (MultiReadHttpServletRequest) request;
        ContentCachingResponseWrapper res = new ContentCachingResponseWrapper(response);
        Byte[] body = res. getContentAsByteArray();
        ...
    }
}
我得到的身体是[]。

3 个答案:

答案 0 :(得分:1)

试试这个:

@Component("dispatcherServlet")
public class FofDisPatcherServlet extends DispatcherServlet {
    @Override
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    MultiReadHttpServletRequest requestWrapper = null;
    try {
        requestWrapper = new MultiReadHttpServletRequest(request);
        super.doDispatch(requestWrapper, new ContentCachingResponseWrapper(request));
    } catch (Exception e) {
        super.doDispatch(request,response);
    }
}

}

public class CustomerInterceptor extends HandlerInterceptorAdapter{
@Override
public void postHandle(..., HttpServletResponse response){
    if (response instanceof ContentCachingResponseWrapper) {
        Byte[] body = ((ContentCachingResponseWrapper)response). getContentAsByteArray();
    }
    ...
}

}

答案 1 :(得分:0)

错误在您的代码中

public class CustomerInterceptor extends HandlerInterceptorAdapter{
    @Override
    public void postHandle((HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView){
        MultiReadHttpServletRequest req = (MultiReadHttpServletRequest) request;
        ContentCachingResponseWrapper res = new ContentCachingResponseWrapper(response);
        Byte[] body = res. getContentAsByteArray();
        ...
    }
}

您正在ContentCachingResponseWrapper中传递请求。

请参阅this问题非常相似的问题。

答案 2 :(得分:0)

几天后。我找到了答案。在CustomerDispatcherServlet我应该添加responseWrapper.copyBodyToResponse()

像这样CustomerDIspatcherServlet

@Component("dispatcherServlet")
public class FofDisPatcherServlet extends DispatcherServlet {
    @Override
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultiReadHttpServletRequest requestWrapper = null;
        try {
            requestWrapper = new MultiReadHttpServletRequest(request);
            if (!(response instanceof ContentCachingResponseWrapper)) {
                ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
                super.doDispatch(requestWrapper, responseWrapper);
                responseWrapper.copyBodyToResponse();
            }else {
                super.doDispatch(requestWrapper, response);
            }
        } catch (Exception e) {
            super.doDispatch(request, response);
        }
    }
}