我有一个WebClient有重试:
webClient.retryWhen(
Retry.fixedDelay(3, Duration.ofSeconds(3))
.filter(this::isRetryable)
)
private boolean isRetryable(Throwable throwable) {
//TODO how access the response body?
}
问题:在重试时如何评估响应体?因为我想再触发器这个webclient调用服务返回http statuscode 200和错误消息"failed"这种反应体内。或者建议我任何替代方法来重新触发基于响应体的值web客户端调用?
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)
);