I am new to project reactor and I am trying to set a List<Object>
inside a Mono<Object>
using a Flux<Object>
.
So I have a Mono<Person>
with an attribute called items
which is a List<Item>
. Then I received from my service layer a Flux<Item>
that I nedd put in items, inside Mono<Person>
.
Here is my code:
public class Person {
List<Item> items;
}
Flux<Item> items = service.getItems();
Person person = new Person();
Mono<Person> monoPerson = Mono.just(person)
.map(p -> p.setItems(items))
... //Here I have received an error because a required type is
... //a List<Items> and I am sendind a Flux<List>
I am frying my mind with this. Some help would be more than appreciate.
CodePudding user response:
You need to think of it as a flow of operations.
What should happens is:
- You receive a flow of items
- You collect them as they arrive
- Once they've all been collected, you assign them to new Person
What gives approximately:
public Mono<Person> createPersonFromReceivedItems() {
Flux<Item> items = service.getItems(); // <1>
Mono<List<Item>> collectedItems = items.collectList(); // <2>
Mono<Person> result = collectedItems.map( itemList -> { // <3>
Person p = new Person();
p.setItems(itemList);
return p;
});
return result;
}