简体   繁体   English

如何在 Spring Webflux 中将 Mono 对象转换为其他 Mono 对象

[英]How to convert Mono object to other Mono object in Spring Webflux

I have a class like this我有这样的课

public class Person {
      private String name;
      private String age;
      private Boolean student;
      ...
      //
      getters and setters
}
public class PersonDto {
      private List<Person> persons
      private Person president
      //
      getters and setters
}

and get data to webclient from external API并从外部 API 获取数据到 webclient

--- omitted ---
final Mono<PersonDto> personDto = wrapperWebClient.getPersonDto(uriComponents, params, PersonDto.class);
Mono<StudentDto> studentDto = convert(personDto);
--- omitted ---

and I want to transform data Mono DTO like below.我想像下面这样转换数据 Mono DTO。

public class Student {
      // no constructors 
      private String name;
      private String age;
      private Boolean student;
      ...
      //
      getters and setters
}
public class StudentDto {
      private List<Student> students;
      private Student represent;
      ...
      //
      getters and setters
}

it's my try这是我的尝试

--- omitted ---
private Mono<StudentDto> convert(Mono<PersonDto> personDto) {
      StudentDto studentDto = new StudentDto();
      personDto.map(
          persons -> {
              studentDto.setStudents(
                      persons.getPersons()
                       .stream().filter(person -> person.isStudent())
                       .collect(toList())
              );
              studentDto.setRepresent(
                      persons.getRepresent().isStudent()
              );
          }
      )
      return ???;
} 

My approach seems to be synchronous.我的方法似乎是同步的。

you use flatMap .你使用flatMap This is basic reactor and you should read through the Getting started reactor before asking on stack overflow.这是基本反应器,您应该在询问堆栈溢出之前通读入门反应器

private Mono<StudentDto> convert(Mono<PersonDto> personDto) {
    return personDto.flatMap(personDto -> {
                final StudentDto studentDto = new StudentDto();
                studentDto.setStudents(
                        persons.getPersons()
                         .stream().filter(person -> person.isStudent())
                         .collect(toList())
                );
                studentDto.setRepresent(
                        persons.getRepresent().isStudent()
                );
                return Mono.just(studentDto);
            })
} 

Just return Mono with map只需使用地图返回Mono

private Mono<StudentDto> convert(Mono<PersonDto> personDto) {
      return personDto.map(
          persons -> {
              StudentDto studentDto = new StudentDto();
              studentDto.setStudents(
                      persons.getPersons()
                       .stream().filter(person -> person.isStudent())
                       .collect(toList())
              );
              studentDto.setRepresent(
                      persons.getRepresent().isStudent()
              );
          }
      );
} 

The main difference between map and flatMap is synchronicity . mapflatMap的主要区别在于同步性

If you want handle another asynchronous one, use flatMap .如果要处理另一个异步,请使用flatMap

Or, if you just want to reformat the output of the Mono/Flux , use map .或者,如果您只想重新格式化Mono/Flux的输出,请使用map

There is transform method in Mono class. Mono类中有transform方法。

return Mono.fromFuture(step1.callFuture(input))
           .doOnSuccess(this::logJsonInput)
           .transform(otherService::calculate);

where在哪里

OtherService {
   public Mono<Result> calculate(Mono<Step1Output> input) {
        //...
   }       
}

如何转换 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.

相关问题 Spring webflux 将实体 class 转换为 Mono DTO ZA8CFDE6331BD59EB2AC96F8911C4B666 - Spring webflux convert Entity class to Mono DTO object 如何在不阻塞的情况下从 Spring Webflux 中的 Mono 对象中提取数据? - How to extract data from a Mono object in Spring Webflux without blocking? 使用 spring webflux 反应式存储库会导致嵌套的 Mono Object - Working with spring webflux reactive repositories results in nested Mono Object Spring-Webflux:从 Mono 中提取 Object 无块() - Spring-Webflux: Extracting Object from Mono without block() 单声道 <Mono<Object> &gt;如何订阅 - Mono<Mono<Object>> how to subscribe 如何使用 Spring Webflux Reactive Java 将 map 几个 Mono 变成一个 object? - How to map several Mono into one object using Spring Webflux Reactive Java? 如何转换 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>? 如何在不阻塞的情况下转换 Mono 对象? - How to convert Mono object without blocking? 如何缓存 Mono 对象 - How to cache Mono object spring-webflux:如何从 Mono 中提取用户定义的 object<t> 或通量<t>从响应没有阻塞?</t></t> - spring-webflux : How to Extract user defined object from Mono<T> or Flux<T> from the response without blocking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM