How to retry in the new HTTP interface in Spring 6 and Spring Boot 3


Spring has introduced the new HTTP interface. For exception handling the documentation states to register a response status handler that applies to all responses performed through the client:

WebClient webClient = WebClient.builder()
    .defaultStatusHandler(HttpStatusCode::isError, resp -> ...)

However, it’s not clear how to handle retries.

In WebClient you could simple use retryWhen():

public Mono<String> getData(String stockId) {
return webClient.get()
  .uri(PATH_BY_ID, stockId)
  .retryWhen(Retry.backoff(3, Duration.ofSeconds(2)));

I’m not sure how to incorporate retries with the Http interfaces.


I figured it out. You need to use an exchange filter. I implemented a similiar solution for a different problem: Adding a retry all requests of WebClient

  TodoClient todoClient() {
    WebClient webClient =
    HttpServiceProxyFactory factory =
    return factory.createClient(TodoClient.class);

private ExchangeFilterFunction retryFilter() {
     return (request, next) ->
                         Retry.fixedDelay(3, Duration.ofSeconds(30))
             .doAfterRetry(retrySignal -> log.warn("Retrying"));

Answered By – pooya72

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published