0

How can we convert the Flux< Employe> to Mono< Customers > object?

Flux< Employe> empFlux = getService(); // It will return list of Employe Employe { private String id; private String info;}

// need transform the empFlux data to Mono< Customers>

public class CusData {

private  String id;
private String  dept;
private  String info;

public String getId() {
    return id;
}

}

public class Customers {

private List<CusData> cusDataList;

public List<CusData> getCusDataList() {
    return cusDataList;
}

public void setCusDataList(List<CusData> cusDataList) {
    this.cusDataList = cusDataList;
}

} public class Employe {

private String id;
private String info;

}

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Sri
  • 59
  • 3

2 Answers2

1

If I understood your code, you must have something like that:

  Mono<Customers> customers = getService().map( employee -> CusData.builder()
                                                                   .id( employee.getId() )
                                                                   .info( employee.getInfo() )
                                                                   .build() )
                                          .collectList()
                                          .map( cusDatas -> Customers.builder()
                                                                     .cusDataList( cusDatas )
                                                                     .build() );
Zrom
  • 1,192
  • 11
  • 24
0

Flux has a handy method collectList() which takes care of performing the transformation for you. I have used the String in the example below.

Code Snippet below.

Flux<String> stringFlux = Flux.just("Spring", "Spring Boot", "Reactive Spring")
                .log();

        Mono<List<String>> stringMono = stringFlux.collectList();