我已经为Spring(使用Spring Boot v1.5)创建了一个过滤器,该过滤器使用OncePerRequestFilter在每个请求上触发,如下所示:
@Component
class LogRequestFilter extends OncePerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(LogRequestFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper wrappedResponse = new ContentCachingResponseWrapper(response);
int responseStatus = wrappedResponse.getStatusCode();
if (responseStatus == 500) {
String requestUri = wrappedRequest.getRequestURI();
byte[] responseBodyBytes = wrappedResponse.getContentAsByteArray();
logger.error("{}, {}, {}", responseStatus, requestUri, new String(responseBodyBytes));
}
filterChain.doFilter(wrappedRequest, response);
}
}
我的问题是当出现错误时,我需要记录响应正文。但是即使用户从Spring接收到以下响应,我也无法从getContentAsByteArray()获取任何数据:
{
"timestamp": 1512745726835,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "No message available",
"path": "/documents/2017-02-17"
}
在调试过程中,我可以看到请求的URI是正确的,但是响应体却丢失了。我该如何解决这个问题?