简体   繁体   中英

How to combine a Mono and a Flux to create one object?

I want to create one object and the object is made up of a Mono and a Flux. Suppose there are 2 services getPersonalInfo and getFriendsInfo . Person needs both services to create an object. Zipping only takes the first element of friends object since there is only one personalInfo as it is Mono, but friendsInfo might have multiple friend objects in them. I want to set the friendsInfo to friend in Person .

class Person{
    String name;
    String age;
    List<Friend> friend;
}

Mono<PersonalInfo> personalInfo = personService.getPerson();// has name and age
Flux<Friend> friendsInfo = friendsService.getFriends();
// here I want to create Person object with personalInfo and friendsInfo
Flux<Person> person = Flux.zip(personalInfo, friendsInfo, (person, friend) -> new Person(person, friend));

From your question I assume you want to create a single person object which contains the name & age populated from your Mono<PersonalInfo> , and the friends list from your Flux<Person> .

Your attempt is pretty close:

Flux<Person> person = Flux.zip(Person, Friend, (person, friend) -> new Person(person, friend));

In particular, the zip operator with the overload that takes two publishers & a combinator is exactly the correct thing to use here. However, a couple of things that need changing:

  • You want a single Person object, so that should be a Mono<Person> (and associated Mono.zip() .
  • As per the comment, you need to convert your Flux into a list, which you can do so with the collectList() oeprator.

So putting that together, you end up with something like:

Mono<Person> person = Flux.zip(personalInfo, friendsInfo.collectList(), (personalInfo, friendsList) -> new Person(personalInfo, friendsList));

...which should give you what you're after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM