Reputation: 400
I am facing a situation where I have to use 2 Mono where second one will be dependent on an Id field of first one and return the response of second one in the body of first Mono.
For example :
Mono<Article> first = fetchArticleById(id);
Mono<Rating> second = fetchRating(article.getRatingId()); //here I will use the response from
first Mono,
then return the result as
/* this is the response of first Mono, the rating field is set by second Mono */
Article {
"id":1234,
"text" : "some text",
"rating" : "5 star", //fetched from second Mono
"ratingId":qq11
}
I have tried
first.map(art -> {
return fetchRating(art.getRatingId());
});
But like this, I can only return response of second Mono.
By trying Map or Flatmap, it only works on the second mono.
Please suggest.
Upvotes: 0
Views: 1440
Reputation: 1082
fetchArticleById(id)
.flatMap(art -> fetchRating(art.getRatingId())
.map(rating -> new Pair(art, rating)));
In the map
function you have access to both article and rating, so you connect them as you need.
Upvotes: 1