1

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

--- 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.

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
EdgarHan
  • 85
  • 1
  • 1
  • 10
  • Does this answer your question? [map vs flatMap in reactor](https://stackoverflow.com/questions/49115135/map-vs-flatmap-in-reactor) – Toerktumlare Jul 08 '21 at 11:50

3 Answers3

0

you use 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);
            })
} 
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
0

Just return Mono with map

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.

If you want handle another asynchronous one, use flatMap.

Or, if you just want to reformat the output of the Mono/Flux, use map.

zzangs33
  • 197
  • 1
  • 15
0

There is transform method in Mono class.

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

where

OtherService {
   public Mono<Result> calculate(Mono<Step1Output> input) {
        //...
   }       
}
alfiogang
  • 435
  • 5
  • 7