【问题标题】:How to log Spring WebClient response如何记录 Spring WebClient 响应
【发布时间】:2018-10-17 08:52:30
【问题描述】:

我是 Spring WebClient 的新手。有人可以建议从另一个 Web 服务记录 REST 请求和响应的最佳方式吗?

我已经在问题中看到了一个记录请求的示例,但还必须记录响应和对 POST 调用的请求。 how to log Spring 5 WebClient call

谢谢。

【问题讨论】:

  • 目前还没有找到比直接解码成字符串,记录日志,然后反序列化更好的方法

标签: spring spring-boot spring-webflux


【解决方案1】:

一种选择是使用onStatus 函数。好处是你可以对不同的状态码做出不同的反应:

.onStatus(HttpStatus::is4xxClientError, res -> {
  res.toEntity(String.class).subscribe(
    entity -> log.warn("Client error {}", entity)
   );
   return Mono.error(new HttpClientErrorException(res.statusCode()));}
 )

但请注意,这将异步记录,这意味着它可能会在您已经记录不同的内容之后记录。我现在正在使用这种方式,但我知道它并不完美,所以我很乐意看到更好的建议。

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 2021-02-10
    • 2018-02-19
    • 2021-12-02
    • 2021-07-21
    • 1970-01-01
    • 2019-10-02
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多