如何使用 Reactor (Spring WebClient) 进行重复调用? [英] How to use Reactor (Spring WebClient) to do a repeat call?
问题描述
我使用 Reactor (Spring5 WebClient) 作为我的反应式编程 API.我有 2 个 REST 端点要调用.第一个的结果将是第二个的参数.对于第二个 API,它将返回一个带有hasMore"值的结果.如果该值为 true,我应该更改分页参数并再次调用第二个 API.演示代码如下:
I am using Reactor (Spring5 WebClient) as my reactive programming API. I have 2 REST endpoint to call. The result of the first one will be the parameter to the second one. For the second API, it will return a result with "hasMore" value. If this value is true, I should change the pagination parameters and call the second API again. The demo code is the following:
client.getApi1()
.map(r -> r.getResult())
.flatMap(p -> client.getApi2(p, 2(page size), 1(page start)))
.subscribe(r -> System.out.println(r.isHasmore()));
如何重复调用第二个 API (getApi2) 直到hasMore"为假.
How to repeat calling the second API (getApi2) until "hasMore" is false.
另外,我需要更改参数页面大小和页面开始
Besides, I need to change the parameters page size and page start
推荐答案
试试这个代码:
AtomicInteger pageCounter = new AtomicInteger(0);
client.getApi1()
.map(r -> r.getResult())
.flatMap(p -> client.getApi2(p, 2(page size), pageCounter.incrementAndGet()))
.repeat()
.takeWhile(r -> r.isHasmore())
.subscribe(r -> System.out.println(r.isHasmore()));
repeat()
无限调用 getApi2.takeWhile(continuePredicate)
传递值,而 continuePredicate (r.isHasmore()
) 返回 true
repeat()
calls getApi2 infinitely.
takeWhile(continuePredicate)
relays values while a continuePredicate (r.isHasmore()
) returns true
这篇关于如何使用 Reactor (Spring WebClient) 进行重复调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!