【问题标题】:How to retrigger a webclient call based on the value inside response body (httpStatus : 200 && getBody().message : "failed")?如何根据响应正文中的值重新触发 Web 客户端调用(httpStatus:200 && getBody().message:“失败”)?
【发布时间】:2022-07-12 01:38:40
【问题描述】:

我有一个重试的 WebClient:

webClient.retryWhen(
   Retry.fixedDelay(3, Duration.ofSeconds(3))
    .filter(this::isRetryable)
)

private boolean isRetryable(Throwable throwable) {
    //TODO how access the response body?
}

问题:如何在重试期间评估响应正文? 因为我想在服务返回 http 状态码 200 并且该响应正文内的错误消息“失败”时重新触发此 webclient 调用。 或者建议我根据响应正文中的值重新触发 webclient 调用的任何替代方法?

【问题讨论】:

    标签: java spring-boot spring-webflux project-reactor spring-webclient


    【解决方案1】:

    retryWhen 仅适用于错误信号,因此您需要根据反序列化的主体返回错误。

    webClient.get()
            .uri("/test")
            .retrieve()
            .bodyToMono(Response.class)
            .flatMap(body -> {
                if (isErrorResponse(body)) {
                    return Mono.error(new ResponseException());
                }
    
                return Mono.just(body);
            })
            .retryWhen(
                    Retry.fixedDelay(3, Duration.ofSeconds(3))
                            .filter(e -> e instanceof ResponseException)
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2018-10-12
      • 2015-11-29
      • 2022-11-17
      • 2020-10-17
      • 1970-01-01
      • 2021-08-23
      • 2021-01-23
      相关资源
      最近更新 更多