我要在一个Flux后面附加一个flatMap,但是如果我添加其他flatMap,则只会返回最后一个。
// Here is an example of the Mono function
private Mono<MyType> appendFirstMono(Group group) {
return Mono.just(group)
.map(MyType::new)
.flatMap(g -> functionZ(group)
.map(g::setField));
}
//This works as expected
public Flux<MyType> function1() {
return returnData(id)
.thenMany(service.getData(id))
.flatMap(this::appendFirstMono);
}
//This does not and only returns the last mono (3rd)
public Flux<MyType> function1() {
return returnData(id)
.thenMany(service.getData(id))
.flatMap(this::appendFirstMono)
.flatMap(this::appendSecondMono)
.flatMap(this::appendThirdMono);
}
//I've attempted to fix with this... Doesn't work as expected.
public Flux<MyType> function1() {
return returnData(id)
.thenMany(service.getData(id))
.flatMap(x -> {
return Flux.merge(
appendFirstMono(x),
appendSecondMono(x),
appendThirdMono(x)
);
});
}
我需要在通量上处理每个Mono函数,但似乎无法使每个函数正常执行并返回。
答案 0 :(得分:1)
您可以尝试concat一步一步地处理我的示例
Flux.concat(getMono(0),getMono(1),getMono(2))
.map(integer -> {
System.out.println(integer);
return integer;
})
.subscribe();
}
private Mono<Integer> getMono(Integer a) {
return Mono.just(a)
;
}
这将打印0,1,2