簡體   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