我有一个春季启动应用程序。现在我需要在拦截器中读取请求和响应。我使用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();
...
}
}
我得到的身体是[]。
答案 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);
}
}
}