Spring Webclient : Lecture 3 - Retry Failed HTTP Calls using Spring WebClient

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 11

  • @mitulvaghela906
    @mitulvaghela906 4 ปีที่แล้ว +2

    You sir are very underrated! So good content. This account needs to blow up!

  • @MRRichiOhrtiz1996
    @MRRichiOhrtiz1996 3 ปีที่แล้ว

    Extremely useful. Much obliged!

  • @vatuska
    @vatuska 4 ปีที่แล้ว +2

    Thanks a lot! you saved my nerves

  • @neutral911
    @neutral911 2 ปีที่แล้ว +1

    Hi bro... I need to carry the same headers of the initial request to the retryable requests as well... Please provide me the information 🙏

  • @remenyiczn
    @remenyiczn 5 ปีที่แล้ว +1

    Hey Dilip, just ran a marathon on your lectures on webclient and it made me understand how it works better, thank you. Could you do a lecture on how to set a "connection timed out" after a given amount of milliseconds and how to break the call and raise a specific exception? I`m trying to create a scenario where the back-end does not respond or it takes longer than usual, and i`d like to interrupt the call after a given time if the server does not respond.

    • @CodewithDilip
      @CodewithDilip  5 ปีที่แล้ว +2

      Hi ,
      Thanks for your feedback. Yes I have plans to do it in the future.
      But If you would like to do it by yourself then the necessary information is available in the below link.
      docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/web-reactive.html#webflux-client-builder-reactor-timeout
      Thanks.
      Dilip Sundarraj.

  • @mwsaab9
    @mwsaab9 3 ปีที่แล้ว

    Hi Dilip, thx for the helpful content. When I add a request logger to the webclient it only outputs one request although 3 retries are executed. This cannot be correct surely? Greetings from Germany

  • @deeplife9654
    @deeplife9654 ปีที่แล้ว

    This guys is so underrated but this guy also should sent to prison for not lunching a full springboot course. I am doing his lamda series but then tried to find if he has a springboot course but really frusted that he did not but he is all about reactive springboot

  • @littlestarsam
    @littlestarsam 4 ปีที่แล้ว +1

    Hello Dilip,
    Its a very nice lecture. I have a scenerio where I have need to retry a request when I get Unauthorized error. I have a access token to pass for each API request which is specific to individual user this access token is valid for 1 day, but sometimes during rare case it is rejected before the expiry time and Unauthorized error is received. In that case I have to retry the request with a new access token.
    The new access token is received by making another Api request by passing a refresh_token value for the user. Now before retry I have to make this request and get the new access_token then with this new access_token make the retry call. I tried using the retry function but it seems to get stucked while making the request to get new access token and then throw the Unauthorized error.
    Would you be able to help me with that....
    private Retry fixedRetry(Token token) {
    return Retry.onlyIf(context -> context.exception() instanceof ClientResponseException && ((ClientResponseException) context.exception()).getStatusCode() == HttpStatus.UNAUTHORIZED.value())
    // .fixedBackoff(Duration.ofSeconds(5))
    .retryOnce()
    .doOnRetry(objectRetryContext ->
    forceRefreshToken(userTokens)
    //calls a api request to get new token
    );
    }
    public Mono create(Token token, final CreateAbc createabc) {

    return client.post()
    .uri(PATH_V2)
    .header(AUTHORIZATION, token.bearer())

    .contentType(APPLICATION_JSON)
    .body(fromObject(createabc))
    .retrieve()
    .onStatus(HttpStatus::isError, response -> response.bodyToMono(String.class)
    .flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(),response.statusCode(),error))))
    .bodyToMono(Abc.class)

    .retryWhen(fixedRetry(token));
    }
    public Mono forceRefreshToken(Token token) {
    return refresh(token).map(service::save);
    }
    public Mono refresh(Token token) {

    return client.post()
    .uri(OAUTH_TOKEN_PATH)
    .header(AUTHORIZATION, basicAuth())
    .body(forRefreshToken(new RefreshToken(token.getRefreshToken())))
    .retrieve()
    .onStatus(HttpStatus::is4xxClientError, response -> response.bodyToMono(String.class)
    .flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(),response.statusCode(),error))))
    .onStatus(HttpStatus::is5xxServerError, response -> response.bodyToMono(String.class)
    .flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(),response.statusCode(),error))))
    .bodyToMono(Token.class)
    ;
    }
    Thanks a lot

  • @ravivedala
    @ravivedala 2 ปีที่แล้ว

    It would be nice if u can update ur git code base to the updated Retry api - reactor.util.retry.Retry