本文介紹了Spring反應返回scanAvailable和Prefetch而不是Object的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個用Spring active開發的rest API,如下所示,當它返回響應時,它確實有scanAvailable和預取,而不是對象詳細信息。對于findAllEmployee,getAllWorkerAddressDetailsByWorkerId下面使用的方法,我們有單獨的API,它確實返回了正確的響應。當我將它們組合在一起時,我不確定哪里出了問題。
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);
});
}
當前響應:
[
{
"xyz": "abc",
"workerAddress": {
"scanAvailable": true,
"prefetch": -1
}
},
{
"xyz": "qwe",
"workerAddress": {
"scanAvailable": true,
"prefetch": -1
}
}
]
預期響應:
[
{
"xyz": "abc",
"workerAddress": {
"key1": "value1",
"key2": "value2"
}
},
{
"xyz": "qwe",
"workerAddress": {
"key1": "value1",
"key2": "value2"
}
}
]
推薦答案
您需要做一些更改。首先,將Flux
放在WorkerDTO
中,并替換為List
,如下所示:
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString(includeFieldNames = true)
public class WorkerDTO {
...
private List<WorkerAddressDTO> workerAddress;
}
然后,您需要按如下方式更改ServiceImpl
代碼:
@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));
});
}
這篇關于Spring反應返回scanAvailable和Prefetch而不是Object的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,