ContentCachingResponseWrapper:如何使用ContentCachingResponseWrapper获取应用程序响应对象(而非httpResponse)

时间:2020-03-24 11:00:35

标签: spring-mvc httpresponse

使用拦截器在Spring Web中验证请求。 我已经扩展了HandlerInterceptorAdapter来实现postHandle方法。 我想检查应用程序响应对象中的值,并相应地执行一些操作。

我尝试了IOUtils来获取应用程序响应对象,但是得到了一个“”字符串。

public class XYZInterceptor extends HandlerInterceptorAdapter {

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
        throws Exception {

    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);

    // need to retrieve application response object

    return;
  }

}

1 个答案:

答案 0 :(得分:1)

在浏览了许多文档之后,我发现输入流/输出流只能访问一次。响应对象到达postHandler之前已经在某处写入了输出流。因此,在postHandle的响应对象中,输出流为空。

如果要访问postHandle中的响应对象,则将setAttribute设置为具有实际响应对象的请求对象。