Spring Webflux -如何使用Tuple 3将三个Flux对象聚合为一个

zpgglvta  于 2023-03-08  发布在  Spring
关注(0)|答案(1)|浏览(165)

我尝试使用Tuple3将3个Flux对象聚合为1个对象。但是,我仍然卡住了。我在网上搜索,看到可以使用Mono.zip(),但不适用于Flux

public class OrderService {

    private final AppWebClient appWebClient;

    public Mono<OrderDetailsAggregate> getAggregatedOrderDetails(List<String> orderNoList1,
            List<String> orderNoList2, List<String> orderNoList3) {
        Flux<Map<String, List<String>>> fluxOne = this.appWebClient.getFluxOne(orderNoList1);
        Flux<Map<String, String>> fluxTwo = this.appWebClient.getFluxTwo(orderNoList2);
        Flux<Map<String, List<String>>> fluxThree = this.appWebClient.getFluxThree(orderNoList3);

        return Mono.zip(fluxOne, fluxTwo, fluxThree).map(this::combine); //error here
    }

    private OrderDetailsAggregate combine(
            Tuple3< Flux<Map<String, List<String>>>, Flux<Map<String, String>>, Flux<Map<String, List<String>>> > tuple) {
        return OrderDetailsAggregate.create(tuple.getT1(), tuple.getT2(), tuple.getT3()); 
    }

}

fluxOnefluxTwofluxThree具有不同的数据类型。

订单详细信息聚集

@Data
@ToString
@AllArgsConstructor(staticName = "create")
public class OrderDetailsAggregate {

    private Flux<Map<String, List<String>>>  fluxOne;
    private Flux<Map<String, String>> fluxTwo;
    private Flux<Map<String, List<String>>> fluxThree;
}

我如何将它们结合起来?
谢谢你。

dgtucam1

dgtucam11#

如果你需要从getAggregatedOrderDetails方法返回Mono<OrderDetailsAggregate>,那么为什么不创建一个没有zip的Mono对象呢?

public Mono<OrderDetailsAggregate> getAggregatedOrderDetails(List<String> orderNoList1,
                                                                 List<String> orderNoList2, List<String> orderNoList3) {
        Flux<Map<String, List<String>>> fluxOne = this.appWebClient.getFluxOne(orderNoList1);
        Flux<Map<String, String>> fluxTwo = this.appWebClient.getFluxTwo(orderNoList2);
        Flux<Map<String, List<String>>> fluxThree = this.appWebClient.getFluxThree(orderNoList3);
        OrderDetailsAggregate aggregate = OrderDetailsAggregate.create(fluxOne, fluxTwo, fluxThree);
        return Mono.just(aggregate);
    }

相关问题