I have two Api's that return two different Mono objects
Mono<User> user1 = api.service1(...);
Mono<UserV2> user2 = api.service2(...);
I want to grab a property from these objects say and set them to another object say SuperUser
SuperUser superUser = new SuperUser();
i want to do something like this
superUser.setProp1(user1.getProp1());
superUser.setProp2(user2.getProp1());
Once that is done, i want to send the superUser Object some method.
CodePudding user response:
Zip the two monos and create the superUser from the tuple
Mono.zip(user1 , user2 ).flatMap(data->{
// get those values as below
data.getT1();
data.getT2();
// set it to superUser
return <your_response_object>;
});
CodePudding user response:
This is somewhat cleaner way to do it :)
SuperUser superUser = new SuperUser();
return Mono.zip(user1, user2).flatMap(data -> {
String prop1 = data.getT1().getProp1();
String prop2 = data.getT2().getProp1();
if(prop1 != null)) {
superUser.setProp1(prop1);
}
if(prop2 != null) {
superUser.setProp2(prop2);
}
return superUser;
});
CodePudding user response:
Use Mono#zipWith like this:
Mono<SuperUser> superUserMono = user1.zipWith(user2)
.map(userTuple -> {
superUser.setProp1(userTuple.getT1().getProp1());
superUser.setProp2(userTuple.getT2().getProp1());
return superUser;
})