![](/img/trans.png)
[英]how to retry Spring WebClient to retry the operation based on the response?
[英]How to access response body in spring WebClient Retry?
我有一个使用重试的WebClient
:
webClient.retryWhen(
Retry.backoff(3, Duration.ofSeconds(3)).filter(this::isRetryable)
)
private boolean isRetryable(Throwable throwable) {
//TODO how access the json body?
}
问题:如何在重试期间评估 json 响应正文? 因为我不仅要根据状态代码做出决定,还要根据返回的错误内容做出决定。
我假设您正在使用 webclient 的retrieve()
方法,如果状态代码发出错误信号,它将始终抛出异常。 您可能想使用exchangeToMono()
代替,它不会在非 2xx 状态代码上引发异常,而是为您提供随心所欲的响应:
WebClient.create("http://my-endpoint.abc/endpoint")
.get()
.exchangeToMono(cr -> cr.bodyToMono(String.class).map(body -> {
//return `Mono.error()` here if invalid - you have access to both body + statuscode (via cr.statusCode())
})).retryWhen(
Retry.backoff(3, Duration.ofSeconds(3)).filter(e -> e instanceof ExceptionThrownAbove)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.