I want to create a Spring-Webflux-Service that combines the data of two sources with a dependency. I'm new to Webflux so I have no idea how to do that. Here is some pseudo code to shows what I`m trying to do:
@Service
public class ServiceB {
private RepositoryB repositoryB;
private ServiceA serviceA;
public Mono<BigDecimal> calculate(UUID id) {
return repositoryB.findById(id)
.flatMap(b -> serviceA.find(b.getId()))
.flatMap((a, b) -> a b);
}
The result of repositoryB
includes the identifier of the data I want to find with serviceA
. At least I want to work on both datasets. What method could I use instead of the first flatMap
?
CodePudding user response:
You may want to use Mono.zipWhen
, to zip the two Monos
into a Tuple
:
public Mono<BigDecimal> calculate(UUID id) {
return repositoryB.findById(id)
.zipWhen(b -> serviceA.find(b.getId())
.map(t -> t.getT1().add(t.getT2()));
}
Also, please note, you can not add two BigDecimals
with
.