【问题标题】:how to render an object with two flux fields without blocking?如何在不阻塞的情况下渲染具有两个通量场的对象?
【发布时间】: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


【解决方案1】:

这应该可行:

return Mono.zip(nodeDTOFlux, edgeDTOFlux)
      .map(tuple2 -> GraphDataDTO.builder().nodeDTO(tuple2.getT1()).edgeDTO(tuple2.getT2()).build())

它创建一个 NodeDTOEdgeDTO 的元组并将其映射到 GraphDataDTO

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 2021-11-03
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多