使用拦截器在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;
}
}
经过大量文档学习后,我发现输入流/输出流只能访问一次。响应对象到达postHandler之前已经在某处写入了输出流。因此,在postHandle的响应对象中,输出流为空。
如果您希望在postHandle中访问响应对象,请使用实际响应对象为请求对象设置setAttribute。