[英]How to access response body in spring WebClient Retry?
I have a WebClient
that makes use of retries:我有一个使用重试的WebClient
:
webClient.retryWhen(
Retry.backoff(3, Duration.ofSeconds(3)).filter(this::isRetryable)
)
private boolean isRetryable(Throwable throwable) {
//TODO how access the json body?
}
Question: how can I evaluate the json response body during retry?问题:如何在重试期间评估 json 响应正文? Because I want to base the decision not only on status code, but on error content returned.因为我不仅要根据状态代码做出决定,还要根据返回的错误内容做出决定。
I assume you're using the retrieve()
method of webclient, which will always throw an exception if the status code signals an error.我假设您正在使用 webclient 的retrieve()
方法,如果状态代码发出错误信号,它将始终抛出异常。 You probably want to use exchangeToMono()
instead, which won't throw an exception on a non-2xx status code, instead providing you the response to do as you please:您可能想使用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.