'Spring Web client timeout fallback

I am having trouble with timeout fallback with spring web client. I want to call another method while timeout error happens. My web client call method:

String response = bkashWebClient
    .webClient()
    .get()
    .uri(queryPaymentUri, paymentId)
    .header("Authorization", bkashToken)
    .header("X-APP-Key", appKey)
    .retrieve()
    .onStatus(HttpStatus::isError, clientResponse ->
        Mono.error(new Exception("Error occurred while calling grant token" + clientResponse.statusCode())))
    .bodyToMono(String.class)
    .timeout(Duration.ofSeconds(30))
    .block();

I want to call a method suppose abc() while a timeout event happens. But I am not able to figure it out. How should I handle such a way so that when a timeout happens it will call another method and the method's return type is not Mono?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source