I'm currently working on a project that involves a bit of reactive programming.
I have 4 different reactive repositories from which I get 4 different Mono<List<SomeType>> in return respectively.
The goal is to combine them into a single Mono<List<GeneralType>> in order to incorporate that into a custom Response to return within a ResponseEntity.ok(). I have already taken care of creating a GeneralType and was successful in converting a single Mono<List<SomeType>>, however, made no further progress.
All repositories have a similar signature:
public Mono<List<SomeType>> findAllByUserId(UUID userId)
The field in my Response that incorporates all different lists as a single one:
private Mono<List<GeneralType>> items;
What my method looks like so far:
public Mono<List<GeneralType>> combineMonos(UUID userId) {
Mono<List<GeneralType>> combo1 = reactiveRepository.findAllByUserId(userId)
.map(list -> list.stream()
.map(GeneralType::new)
.collect(Collectors.toList()));
return combo1; // works just fine
}
All the other lists have pretty much the same approach, but putting them together into a single Mono<List> is a problem.
I've tried the following:
return Flux.merge(combo1.flatMapMany(Flux::fromIterable), combo2.flatMapMany(Flux::fromIterable)).collectList();
But with that, the IDE urges to change the return type to Flux<Object>.
Also, some lists can be empty, so I'm not sure if zip() is an option here. I've read that it will return everything as empty if at least a single result is empty.
So the question is how can that be done in an efficient way without block() everywhere?
zipif it has a value of empty list. But mono cannot continuezipif it has empty value. You can tryzip()operator to combine allMonointo one. - shafayat hossain