Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅记录来自WebClient的POST ant修补程序请求_Java_Spring Boot_Rest_Logging_Webclient - Fatal编程技术网

Java 如何仅记录来自WebClient的POST ant修补程序请求

Java 如何仅记录来自WebClient的POST ant修补程序请求,java,spring-boot,rest,logging,webclient,Java,Spring Boot,Rest,Logging,Webclient,我正在注册用于记录WebClient响应的过滤器,但我只想记录POST和PATCH请求(没有太多我不感兴趣的GET请求)。如何只记录特定的帖子和补丁响应 WebClient bean: @Bean public WebClient sfWebClient() { return WebClient.builder() .filter(logResponse()) .build(); } 日志响应过滤器: ExchangeFilterFunc

我正在注册用于记录WebClient响应的过滤器,但我只想记录POST和PATCH请求(没有太多我不感兴趣的GET请求)。如何只记录特定的帖子和补丁响应

WebClient bean:

  @Bean
  public WebClient sfWebClient() {
    return WebClient.builder()
        .filter(logResponse())
        .build();
  }
日志响应过滤器:

  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);
    });
  }

我认为您可以像这样实现自己的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;
        });
    );

我认为您可以像这样实现自己的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;
        });
    );

这很好,但请将
返回Mono.just(response)
更改为
返回响应
。语句
next.exunge(request)
已经返回了
Mono
,因此您的整个筛选函数将返回
Mono
@sergvasyslchak,非常感谢。它工作得非常好:)。感谢you@PiotrSupel不客气:)很好,但请将
返回Mono.just(response)
更改为
返回response
。语句
next.exunge(request)
已经返回了
Mono
,因此您的整个筛选函数将返回
Mono
@sergvasyslchak,非常感谢。它工作得非常好:)。感谢you@PiotrSupel不客气:)