亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據響應重試 WebClient

根據響應重試 WebClient

飲歌長嘯 2023-08-16 16:16:37
我創建了一個 Spring webflux webclient。我想根據我的響應重復相同的操作。例如:如果數據仍然為空,我想重試獲取數據。怎么做 ?Flux<Data> data = webClient.get()                .uri("/api/users?page=" + page)                .retrieve()                .flatMap(o -> {                  o.subscribe(data -> {                      if(data == null) {                         // WHAT TO DO HERE, TO REPEAT THE SAME CALL ?                         o.retry();                      }                });                return o;            })            .bodyToFlux(Data.class);
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

您可以使用retry(Predicate<? super Throwable> retryMatcher),它將根據可拋出條件重試該操作。


在下面的代碼中,如果從客戶端接收到的數據為空,我將返回 Mono.error,然后根據重試中的錯誤條件再次執行上述操作。


您還可以限制重試次數,


retry(long numRetries, Predicate<? super Throwable> retryMatcher)


final Flux<Data> flux = WebClient.create().get().uri("uri").exchange().flatMap(data -> {

      if (data == null)

        return Mono.error(new RuntimeException());

      return Mono.just(data);


    }).retry(throwable -> throwable instanceof RuntimeException)

        .flatMap(response -> response.bodyToMono(Data.class)).flux();


查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號