title: Spring WebClient : Call method in retry
body: <p>I have been looking for a solution to the following use case without success, I hope someone can help :</p>
  <p>Assuming the following use case. I need to call a customer Api (<code>customerApi</code>) and this api needs a <code>Bearer</code> token which may have expired when I call <code>customerApi</code>. If the token has expired, the <code>customerApi</code> returns a <code>401</code> response.</p>
  <p>What, I want to do is to retry only once if I received a <code>401</code> and call the method to get a new <code>Bearer</code> token. If the retry still returns <code>401</code>, I need to throw an <code>Exception</code></p>
  <p>The method to get a <code>Bearer</code> token :</p>
  <pre><code>private String getToken() {
      return oAuthService.getToken();
  }
  </code></pre>
  <p>And the <code>webClient</code> usage to call <code>customerApi</code> (<code>customerWebClient</code> is a bean created with <code>WebClient.Builder</code>) :</p>
  <pre><code>public Customer getCustomerById(String customerId, String token) {
          return customerWebClient.get()
              .uri(&quot;myurl/customers/{customerId}, customerId)
              .headers(httpHeaders -&gt; {
                  httpHeaders.add(HttpHeaders.AUTHORIZATION, &quot;Bearer &quot; + token);
              })
              .retrieve()
              .bodyToMono(Customer.class)
              .onErrorResume(WebClientResponseException.NotFound.class, notFound -&gt;
                          Mono.error(new MyCustomException()))
              .block();
      }
  </code></pre>
  <p>It seems that <code>retryWhen</code> can only be used to upgrade timeout. So I hope that someone know how to achieve this use case ^^</p>
  <p>Thanks for your help :)</p>
  <p><strong>EDIT :</strong></p>
  <p>I tried to use <code>retryWhen(Retry.onlyIf(...))</code> from <code>reactor-extra</code> but the good old <code>retryWhen</code> from this package is now deprecated (solution based on : <a href="https://stackoverflow.com/questions/50752021/adding-a-retry-all-requests-of-webclient">Adding a retry all requests of WebClient</a>)</p>