在我的应用程序中,我有2个过滤器:
1)首先,它引发异常,我打电话给response.sendError(UNAUTHORIZED.getStatusCode(), "My message");
2)第二个用于记录对数据库的请求和响应。
我有一个列“ error_message”,我想将来自过滤器1的错误消息保存在过滤器2中。如何在weblogic上执行此操作?我有javax.servlet.http.HttpServletResponse,我只能取得状态:
response.getStatus()
使用Spring我可以打电话
ServletRequestContext.current().getErrorMessage()
,但是当我使用weblogic时不起作用。
答案 0 :(得分:0)
与其创建自定义HttpServletResponseWrapper,还可以使用ContentCachingResponseWrapper,因为它提供了getContentAsByteArray()方法。
更多内容-How to read and copy the HTTP servlet response output stream content for logging
public void doFilterInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = servletRequest;
HttpServletResponse response = servletResponse;
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper responseWrapper =new ContentCachingResponseWrapper(response);
try {
super.doFilterInternal(requestWrapper, responseWrapper, filterChain);
} finally {
byte[] responseArray=responseWrapper.getContentAsByteArray();
String responseStr=new String(responseArray,responseWrapper.getCharacterEncoding());
System.out.println("string"+responseStr);
/*It is important to copy cached reponse body back to response stream
to see response */
responseWrapper.copyBodyToResponse();
}
}