使用Spring启动执行器跟踪,我正在尝试添加跟踪请求&反应机构。我已经关注帖子How to include JSON response body in Spring Boot Actuator's Trace?并创建了一个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());
}
}
答案 0 :(得分:0)
使用responseWrapper.copyBodyToResponse();
took from。
@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);
}