简体   繁体   中英

Spring reactive returns scanAvailable and prefetch instead of object

I have a REST API developed with Spring Reactive as below and when it returns the response it does have scanAvailable and prefetch instead of object details. We have individually APIs for the used methods below findAllEmployment, getAllWorkerAddressDetailsByWorkerId and it do return the proper response. Not sure what is wrong when I have combined them.

WorkerDTO.java

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString(includeFieldNames = true)
public class WorkerDTO {
    
    ...

    private Flux<WorkerAddressDTO> workerAddress;

}

ServiceImpl.java

@Override
public Flux<WorkerDTO> method() {
  Flux<EmploymentDTO> employmentDTOFlux = findAllEmployment();
  Flux<WorkerDTO> workerDTOFlux = getWorkerDetailsWithEmploymentStatus(employmentDTOFlux);
  return workerDTOFlux.flatMap(workerDTO -> {
    workerDTO.setWorkerAddress(getAllWorkerAddressDetailsByWorkerId(workerDTO.getWorkerId()).map(workerAddressDTO -> workerAddressDTO));
     return Flux.just(workerDTO);
  });
}

Current Response:

[
    {
        "xyz": "abc",
        "workerAddress": {
            "scanAvailable": true,
            "prefetch": -1
        }
    },
    {
       "xyz": "qwe",
        "workerAddress": {
            "scanAvailable": true,
            "prefetch": -1
        }
    } 
]

Expected Response:

[
    {
        "xyz": "abc",
        "workerAddress": {
            "key1": "value1",
            "key2": "value2"
        }
    },
    {
       "xyz": "qwe",
        "workerAddress": {
            "key1": "value1",
            "key2": "value2"
        }
    } 
]

You need to do some changes. First, drop Flux inside WorkerDTO and replace it with List as follows:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString(includeFieldNames = true)
public class WorkerDTO {
    
    ...

    private List<WorkerAddressDTO> workerAddress;
}

Then, you need to change your ServiceImpl code as follows:

@Override
public Flux<WorkerDTO> method() {
    Flux<EmploymentDTO> employmentDTOFlux = findAllEmployment();
    Flux<WorkerDTO> workerDTOFlux = getWorkerDetailsWithEmploymentStatus(employmentDTOFlux);
    return workerDTOFlux.flatMap(workerDTO -> {
         return getAllWorkerAddressDetailsByWorkerId(workerDTO.getWorkerId()).collectList()
            .map(workerAddresses -> workerDTO.setWorkerAddress(workerAddresses));
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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