繁体   English   中英

Spring Boot执行器在跟踪中添加请求主体会抛出HttpMessageNotReadableException

[英]Spring boot actuator adding request body in trace throws HttpMessageNotReadableException

使用Spring Boot执行器跟踪,我试图在请求和响应主体中添加跟踪。 我已经关注了如何在Spring Boot Actuator的Trace中包括JSON响应正文的文章。 并创建了RequestTraceFilter类,而无需使用logback。 但是,如果我发出请求,则会在控制台中出现异常“无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文:”

另外,一旦添加此过滤器,swagger-ui.html将无法加载。

我错过了下课吗?

@Component
public class RequestTraceFilter extends WebRequestTraceFilter {

private static final String RESPONSE_BODY = "resBody";
private static final String REQUEST_BODY = "reqBody";

public RequestTraceFilter(TraceRepository repository, TraceProperties properties) {
    super(repository, properties);
}

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
    filterChain.doFilter(requestWrapper, responseWrapper);
    request.setAttribute(REQUEST_BODY, getRequestBody(requestWrapper));
    request.setAttribute(RESPONSE_BODY, getResponseBody(responseWrapper));
    super.doFilterInternal(requestWrapper, responseWrapper, filterChain);
}


private String getRequestBody(ContentCachingRequestWrapper request) {
    ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
    String characterEncoding = wrapper.getCharacterEncoding();
    return getPayload(wrapper.getContentAsByteArray(), characterEncoding);
}

@Override
protected Map<String, Object> getTrace(HttpServletRequest request) {
    Map<String, Object> trace = super.getTrace(request);
    Object requestBody = request.getAttribute(REQUEST_BODY);
    Object responseBody = request.getAttribute(RESPONSE_BODY);
    if(requestBody != null) {
        trace.put(REQUEST_BODY, (String) requestBody);
    }
    if(responseBody != null) {
        trace.put(RESPONSE_BODY, (String) responseBody);
    }
    return trace;
}

public String getPayload(byte[] buf, String characterEncoding) {
    String payload = null;
        if (buf.length > 0) {
            try {
                payload = new String(buf, 0, buf.length, characterEncoding);
            }
            catch (UnsupportedEncodingException ex) {
                payload = "[unknown]";
            }
        }
    return payload;
}

private String getResponseBody(ContentCachingResponseWrapper response) {
    ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
    return getPayload(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
}

}

使用responseWrapper.copyBodyToResponse();

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
    filterChain.doFilter(requestWrapper, responseWrapper);
    responseWrapper.copyBodyToResponse();
    request.setAttribute(REQUEST_BODY, getRequestBody(requestWrapper));
    request.setAttribute(RESPONSE_BODY, getResponseBody(responseWrapper));
    super.doFilterInternal(requestWrapper, responseWrapper, filterChain);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM