如何只记录来自webclient的post ant补丁请求

8xiog9wr  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(381)

我正在注册过滤器来记录来自webclient的响应,但是我只想记录post和patch请求(没有太多我不感兴趣的get请求)。如何只记录特定的post和patch响应?
webclient bean:

@Bean
  public WebClient sfWebClient() {
    return WebClient.builder()
        .filter(logResponse())
        .build();
  }

logresponse筛选器:

ExchangeFilterFunction logResponse() {
    return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
      if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("Request finished with the status: ").append(clientResponse.statusCode());
        log.debug(sb.toString());
      }
      return Mono.just(clientResponse);
    });
  }
lmvvr0a8

lmvvr0a81#

我认为您可以像这样实现自己的ExchangeFilter功能:

WebClient.builder().filter((request, next) ->
        next.exchange(request).map(response -> {
            if ((request.method() == HttpMethod.POST || request.method() == HttpMethod.PATCH) && log.isDebugEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("Request finished with the status: ").append(response.statusCode());
                log.debug(sb.toString());
            }
            return response;
        });
    );

相关问题