we have a server to retrieve a OAUTH token, and the oauth token is added to each request via WebClient.filter method e.g
webClient
.mutate()
.filter((request, next) -> tokenProvider.getBearerToken()
.map(token -> ClientRequest.from(request)
.headers(httpHeaders -> httpHeaders.set("Bearer", token))
.build()).flatMap(next::exchange))
.build();
TokenProvider.getBearerToken returns Mono<String> since it is a webclient request (this is cached)
I want to have a retry functionality that on 401 error, will invalidate the token and try the request again I have this working like so
webClient.post()
.uri(properties.getServiceRequestUrl())
.contentType(MediaType.APPLICATION_JSON)
.body(fromObject(createRequest))
.retrieve()
.bodyToMono(MyResponseObject.class)
.retryWhen(retryOnceOn401(provider))
private Retry<Object> retryOnceOn401(TokenProvider tokenProvider) {
return Retry.onlyIf(context -> context.exception() instanceof WebClientResponseException && ((WebClientResponseException) context.exception()).getStatusCode() == HttpStatus.UNAUTHORIZED)
.doOnRetry(objectRetryContext -> tokenProvider.invalidate());
}
is there a way to move this up to the webClient.mutate().....build() function? so that all requests will have this retry facility?
I tried adding as a filter but it didn't seem to work e.g.
.filter(((request, next) -> next.exchange(request).retryWhen(retryOnceOn401(tokenProvider))))
any suggestions of the best way to approach this? Regards
log()
operator? – Dated