简体   繁体   English

如何将 Flux 设置为 Mono 内的列表 object

[英]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:应该发生的是:

  1. You receive a flow of items您收到了一系列物品
  2. You collect them as they arrive你在它们到达时收集它们
  3. Once they've all been collected, you assign them to new Person收集完所有内容后,您将它们分配给新的 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;
}

如何转换 Mono <list<object> &gt; 到通量<object> ?<div id="text_translate"><p> 我刚开始使用反应式,所以如果问题没有意义,请纠正我。</p><p> 我正在创建一个 rest controller 和 Spring WebFlux + Reactive Z206E3718AF092FD1DZ12F8 数据库中的以下文档结构 ICAE712FD1DZ12F8。</p><pre> { "_id": { "$oid": "5ee350839d3d4e34f0790566" }, "customerId": "7777", "contacts": [{ "_id": { "$oid": "5ee350839d3d4e34f0790565" }, "name": "Alice", "mobileNumbers": "0123456789" }, { "_id": { "$oid": "5ee3508a9d3d4e34f0790567" }, "name": "Tom", "mobileNumbers": "1123456789" } ], "_class": "com.demo.contact.model.Customer" }</pre><p> 此示例文档有一位客户及其联系人。 从客户的联系人列表中搜索时,我正在尝试获取 Flux。</p><pre> public Mono&lt;List&lt;Contact&gt;&gt; searchContacts(String customerId, String searchCriteria) { return customerRepository.findById(customerId).map(existingCustomer -&gt; { List&lt;Contact&gt; contacts= existingCustomer.getContacts().stream().filter( // some filtering code ).sorted(Comparator.comparing(Contact::getName)).collect(Collectors.toList()); return contacts; }); }</pre><p> 问题是如何更改这段代码以获得Flux&lt;Object&gt;而不是Mono&lt;List&lt;Object&gt;&gt; ? </p></div></object></list<object> - How to convert a Mono<List<Object>> to a Flux<Object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换 Mono <list<object> &gt; 到通量<object> ?<div id="text_translate"><p> 我刚开始使用反应式,所以如果问题没有意义,请纠正我。</p><p> 我正在创建一个 rest controller 和 Spring WebFlux + Reactive Z206E3718AF092FD1DZ12F8 数据库中的以下文档结构 ICAE712FD1DZ12F8。</p><pre> { "_id": { "$oid": "5ee350839d3d4e34f0790566" }, "customerId": "7777", "contacts": [{ "_id": { "$oid": "5ee350839d3d4e34f0790565" }, "name": "Alice", "mobileNumbers": "0123456789" }, { "_id": { "$oid": "5ee3508a9d3d4e34f0790567" }, "name": "Tom", "mobileNumbers": "1123456789" } ], "_class": "com.demo.contact.model.Customer" }</pre><p> 此示例文档有一位客户及其联系人。 从客户的联系人列表中搜索时,我正在尝试获取 Flux。</p><pre> public Mono&lt;List&lt;Contact&gt;&gt; searchContacts(String customerId, String searchCriteria) { return customerRepository.findById(customerId).map(existingCustomer -&gt; { List&lt;Contact&gt; contacts= existingCustomer.getContacts().stream().filter( // some filtering code ).sorted(Comparator.comparing(Contact::getName)).collect(Collectors.toList()); return contacts; }); }</pre><p> 问题是如何更改这段代码以获得Flux&lt;Object&gt;而不是Mono&lt;List&lt;Object&gt;&gt; ? </p></div></object></list<object> - How to convert a Mono<List<Object>> to a Flux<Object>? 如何使Flux脱离Mono内部的列表? - How to get Flux out of list that is inside Mono? 如何将 Mono 中的列表属性作为通量操作? - How to manipulate a list attribute inside Mono as a flux? 如何转换通量<list<object> &gt; 进Mono <list<object> &gt; </list<object></list<object> - How to convert Flux<List<Object>> into Mono<List<Object>> 助焊剂进入 Mono 列表 Object - 项目反应堆 - Flux into Mono List Object - project reactor 如何在 Mono/Flux 中调用 WebClient 方法? - how to call WebClient methods inside Mono/Flux? 如何结合 Mono 和 Flux 创建一个 object? - How to combine a Mono and a Flux to create one object? Mono如何转换<list<string> > 进入助焊剂<string></string></list<string> - How to convert Mono<List<String>> into Flux<String> 如何将Mono中的嵌套列表转换为Flux? - How to convert nested list in Mono to Flux? 在 Flux Flatmap 中处理 Mono - Handling Mono Inside Flux Flatmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM