[英]How to set a Flux into a list object inside a Mono
I am new to project reactor and I am trying to set a List<Object>
inside a Mono<Object>
using a Flux<Object>
.我是项目反应堆的新手,我正在尝试使用
Flux<Object>
> 在Mono<Object>
中设置List<Object>
Object> 。
So I have a Mono<Person>
with an attribute called items
which is a List<Item>
.所以我有一个
Mono<Person>
带有一个名为items
的属性,它是一个List<Item>
。 Then I received from my service layer a Flux<Item>
that I nedd put in items, inside Mono<Person>
.然后我从我的服务层收到一个
Flux<Item>
,我需要将它放入项目中,在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.
一些帮助将不胜感激。
You need to think of it as a flow of operations.您需要将其视为操作流程。
What should happens is:应该发生的是:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.