How do I convert Flux<Object> to Flux<ObjectDto> to Mono<ListOfDto>?

49 Views Asked by At

I have a problem with data conversion in reactive Spring. I have two dtos: ObjectDto and ListOfDtos that has a constructor ListOfDtos(List<ObjectDto> dtos). There are also mapper ObjectMapper:

public static Mono<ObjectDto> toObjectDto(Object object) {
    return Mono.just(object)
        .map(o - > new ObjectDto(o.getField1(), o.getField2(), ...));
}

In the service, I have to get a list of objects by field and return ListOfDtos. How can I do this?

The most I could achieve was to return Flux<ObjectDto> but I need to return Mono<ListOfDtos>

public Flux<ObjectDto> getAllObjectsByField(String field) {
    return Mono.just(field)
        .flatMapMany(field - > objectRepository.findByField(field))
        .flatMap(ObjectMapper::toObjectDto);
}

I will be grateful for your help!

0

There are 0 best solutions below