我想让actuator在/actuator/httptrace中导出响应主体和请求主体,我做了一些定制类来实现这一点,但它根本不起作用。
习俗 HttpTraceRepository
(供将来使用):
@Repository
public class CustomTraceRepository implements HttpTraceRepository {
AtomicReference<HttpTrace> lastTrace = new AtomicReference<>();
@Override
public List<HttpTrace> findAll() {
return Collections.singletonList(lastTrace.get());
}
@Override
public void add(HttpTrace trace) {
if ("GET".equals(trace.getRequest().getMethod())) {
lastTrace.set(trace);
}
}
}
还有一种习俗 HttpTraceFilter
,魔法应该发生的地方:
@Component
public class RequestFilter extends HttpTraceFilter {
RequestFilter(CustomTraceRepository repository, HttpExchangeTracer tracer) {
super(repository, tracer);
}
@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("test","testing");
request.setAttribute("requestbody", requestWrapper.getRequest());
request.setAttribute("responsebody", responseWrapper.getResponse());
super.doFilterInternal(requestWrapper, responseWrapper, filterChain);
}
}
但两者都不是 "requestbody"
, "responsebody"
或者 "test"
属性显示在痕迹上。我不知道该怎么办。
在别人说我应该通过之前 request
以及 response
而不是 requestWrapper
以及 responseWrapper
在 super.doFilterInternal
我已经试过了。我的自定义属性就是不起作用。
暂无答案!
目前还没有任何答案,快来回答吧!