简体   繁体   English

在 Spring 引导中使用 Mono 迭代 Flux 和 concat

[英]Iterate Flux and concat with Mono in Spring Boot

I do have a fetchEmployment() method which does successfully fetch the records and I am iterating over it to fetch the Mono object based on the worker id which successfully returns the object but I am not able to create the final Flux which should consist of WorkerDTO (list of WorkerDTO for normal Spring Boot application) but it returns the empty object ie [] . I do have a fetchEmployment() method which does successfully fetch the records and I am iterating over it to fetch the Mono object based on the worker id which successfully returns the object but I am not able to create the final Flux which should consist of WorkerDTO (正常 Spring 引导应用程序的WorkerDTO列表)但它返回空的 object 即[]

@Override
public Flux<WorkerDTO> method() {
    Flux<EmployeeDTO> employmentDTOFlux = fetchEmployment();
    Flux<WorkerDTO> workerDTOFlux = Flux.empty();

    employmentDTOFlux.flatMap(employmentDTO -> {
        Mono<WorkerDTO> worker = workerService.findWorkerById(employmentDTO.getWorkerId());
        return Flux.concat(workerDTOFlux, Flux.from(worker));
    });

    return workerDTOFlux;
}

I think you can rewrite this as something as simple as:我认为您可以将其重写为简单的:

return fetchEmployment()
       .map(EmployeeDTO::getWorkerId)
       .map(workerService::findWorkerById);

The following is simpler and should work as intended:以下更简单,应该按预期工作:

@Override
public Flux<WorkerDTO> method() {
    return fetchEmployment().flatMap(employmentDTO -> {
        workerService.findWorkerById(employmentDTO.getWorkerId());
    });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM