【发布时间】:2021-12-29 03:30:19
【问题描述】:
我想渲染一个由两个单声道或通量元素组成的对象(在代码 sn-p 下方):
Mono<List<NodeDTO>> nodeDTOFlux = this.webClient
.get()
.uri(NODES_WITH_LIMIT + limit)
.retrieve()
.onStatus(HttpStatus::isError,
response -> response.bodyToMono(String.class).flatMap(
msg -> Mono.error(new ApiCallException(msg, response.statusCode())))
)
.bodyToFlux(new ParameterizedTypeReference<Node>() {
}).map(node -> nodeMapper.toNodeDTO(node))
.collectList();
Mono<List<EdgeDTO>> edgeDTOFlux = this.webClient
.get()
.uri(EDGES_WITH_LIMIT + limit)
.retrieve()
.onStatus(HttpStatus::isError,
response -> response.bodyToMono(String.class).flatMap(
msg -> Mono.error(new ApiCallException(msg, response.statusCode())))
)
.bodyToFlux(new ParameterizedTypeReference<Edge>() {
}).map(edge -> edgeMapper.toEdgeDTO(edge))
.collectList();
我尝试了 zip() 方法,但这不是我的目标 我试图返回这样的对象
GraphDataDTO graphDataDTO = new GraphDataDTO();
graphDataDTO.setEdgeDTOS(edgeDTOFlux);
graphDataDTO.setNodeDTOS(nodeDTOFlux);
我的控制台中有结果,但对象返回 { “nodeDTOS”:{ “扫描可用”:真 }, “边缘DTOS”:{ “扫描可用”:真 } } 返回是在获得所有通量之前完成的。有没有不阻塞的解决方案! 提前致谢。
【问题讨论】:
-
你用 zip() 试过什么?
-
return Mono.zip(nodeDTOFlux, edgeDTOFlux).map( Tuple2::toList ).map(objects -> ) .flatMapMany(Flux::fromIterable);
-
它将所有东西组合在同一个对象中,但我想得到像 { "nodeDTOS": [ { }, { } ..], "edgeDTOS": [ { }, { } .. ] }
标签: spring mono reactive-programming spring-webflux flux