Java 如何仅记录来自WebClient的POST ant修补程序请求
我正在注册用于记录WebClient响应的过滤器,但我只想记录POST和PATCH请求(没有太多我不感兴趣的GET请求)。如何只记录特定的帖子和补丁响应 WebClient bean: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
@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不客气:)