我正在使用spring 4和过滤器我在servletResponse中得到了一些错误响应。它包含带有错误消息的outputstream。 如何在Outputstream中获取此错误消息并审核到我的应用程序?
try{
filterChain.doFilter(wrappedRequest, servletResponse);
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
LOGGER.info("Basic Authenticate HttpServletResponse code {}"+httpServletResponse.getStatus());
httpServletResponse.getOutputStream();
// I need to read this outputstream, its contains error message.?
}catch(Exception e){
LOGGER.error("Error Basic Authenticate Error :{}", e);
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
AtomicInteger statusCode = new AtomicInteger(httpServletResponse.getStatus());
Response response = ResponseUtil.populateResponse(statusCode, e.getMessage().toString(), 0);
httpServletResponse.sendError(httpServletResponse.getStatus(), e.getMessage());
auditService.updateRestAuditRecord(auditId,StringUtils.EMPTY, response);
}
答案 0 :(得分:0)
关键是HttpServletResponseWrapper
装饰器类。根据您的特定需求调整这些步骤:
HttpServletResponseWrapper
,覆盖getOutputStream()
并返回流式传输字节缓冲区的ServletOutputStream
扩展名。HttpServletResponseWrapper
传递给链中的下一个下游过滤器。