【发布时间】:2021-02-27 20:14:00
【问题描述】:
我有这样的要求。
Flux<Integer> s1 = .....;
s1.flatMap(value -> anotherSource.find(value));
当anotherSource.find 给我第一个空的时候,我需要一种方法来阻止这个 s1。该怎么做?
注意:
一种可能的解决方案是抛出错误然后捕获它以停止。
anotherSource.find(value).switchIfempty(Mono.error(..))
我正在寻找比这更好的解决方案。
【问题讨论】:
-
其他选项是定义某种空对象,然后使用
takeWhile运算符:s1.flatMap(value -> anotherSource.find(value).defaultIfEmpty(MY_SPECIAL_EMPTY_OBJECT)).takeWhile(i -> i != MY_SPECIAL_EMPTY_OBJECT) -
另一个 hack.. 好。所以没有特定的运算符!?
标签: spring-webflux project-reactor