自定义执行器请求

9ceoxa92  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(287)

我想让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 以及 responseWrappersuper.doFilterInternal 我已经试过了。我的自定义属性就是不起作用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题