2

I have a WebClient that makes use of retries:

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? Because I want to base the decision not only on status code, but on error content returned.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

1

I assume you're using the retrieve() method of webclient, which will always throw an exception if the status code signals an error. 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:

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)
        );
Michael McFadyen
  • 2,675
  • 11
  • 25
Michael Berry
  • 70,193
  • 21
  • 157
  • 216