第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用新參數(shù)重復 WebClient 獲取

使用新參數(shù)重復 WebClient 獲取

慕哥6287543 2022-06-08 16:55:16
我正在開發(fā)一個 Spring WebFlux 應用程序,并且我有一個 Web 適配器,用于調(diào)用我使用的外部 api 方法。其中一個 api 使用帶有 rel="next" 的鏈接標頭具有分頁結果。我需要調(diào)用這個 api,直到下一個鏈接不存在,但我不確定如何實現(xiàn)這一點。以下是我目前正在使用的基本調(diào)用:public Flux<ItemDto> getAllItems(){   return webClient.get() //The headers and base url for the api are defined in the constructor        .uri("/api/items?limit=200") //limit is the number of items returned with 200 being the maximum        .retrieve()        .bodyToFlux(Map.class)        .map(ItemConverter::mapValueToItemDto);//This is just a conversion method that handles mapping}我需要的是能夠重復此調(diào)用,但添加一個請求參數(shù),該參數(shù)基于具有“下一個”rel 值的鏈接標頭的一部分。我已經(jīng)看到提到使用擴展或重復,但我不確定如何準確使用它們。我知道使用 exchange 是獲取 clientResponse 所必需的,因此我可以獲得標頭。這個問題可能相當模糊,所以如果需要,我可以提供任何澄清。
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經(jīng)驗 獲得超5個贊

經(jīng)過大量試驗和錯誤并找到特定部分的解決方案后,我找到了適合我的解決方案。


public Flux<ItemDto> getAllItems() {

    webClient.get()

            .uri("/api/items?limit=1")//Used one to test

            .exchange()

            .expand(clientResponse -> {

                List<String> links = clientResponse.headers().asHttpHeaders().getValuesAsList("LINK");

                if(links.stream().anyMatch(link->link.contains("rel=\"next\""))){

                    for (String link : links){

                        if (link.contains("rel=\"next\"")){

                            return webClient.get()

                                    .uri("/api/items?limit=1&" + link.substring(link.indexOf("after="), link.indexOf("&")))

                                    .exchange();

                        }

                    }

                }

                return Flux.empty();

            })

            .flatMap(clientResponse ->

                    clientResponse.bodyToFlux(Map.class)

                            .map(ItemConverter::mapValueToItemDto));

}

不需要任何遞歸。只是更合適地使用擴展?,F(xiàn)在,其中的一部分(見下文)實際上可以分解成自己的方法,但由于它只有幾行,我選擇不這樣做。


webClient.get()

   .uri("/api/items?limit=1" + after)//This after bit would be like what was passed as an argument before

   .exchange();


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號