I have been looking for a solution to the following use case without success, I hope someone can help :
Assuming the following use case. I need to call a customer Api (customerApi) and this api needs a Bearer token which may have expired when I call customerApi. If the token has expired, the customerApi returns a 401 response.
What, I want to do is to retry only once if I received a 401 and call the method to get a new Bearer token. If the retry still returns 401, I need to throw an Exception
The method to get a Bearer token :
private String getToken() {
return oAuthService.getToken();
}
And the webClient usage to call customerApi (customerWebClient is a bean created with WebClient.Builder) :
public Customer getCustomerById(String customerId, String token) {
return customerWebClient.get()
.uri("myurl/customers/{customerId}, customerId)
.headers(httpHeaders -> {
httpHeaders.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
})
.retrieve()
.bodyToMono(Customer.class)
.onErrorResume(WebClientResponseException.NotFound.class, notFound ->
Mono.error(new MyCustomException()))
.block();
}
It seems that retryWhen can only be used to upgrade timeout. So I hope that someone know how to achieve this use case ^^
Thanks for your help :)
EDIT :
I tried to use retryWhen(Retry.onlyIf(...)) from reactor-extra but the good old retryWhen from this package is now deprecated (solution based on : Adding a retry all requests of WebClient)
The method
has been deprecated and now the preferred method is
So, you can modify your code to something like this to make it work with the new
retryWhenHere's a working example using https://httpbin.org/
Also, I do not think the way you're trying to manipulate headers in retry to add authorization token is the right way of achieving this. You must come up with a better solution/design.