Spring boot - 如何将500响应主体记录到控制台

时间:2017-12-08 15:12:31

标签: java spring logging

我已经为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是正确的,但是缺少响应体。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

在阅读doFilter的内容之前,请先移动wrappedResponse方法调用。 最后致电wrappedResponse.copyBodyToResponse()