【问题标题】:Stream<Mono<T>> to Flux<T> in spring reactor将 <Mono<T>> 流到 Spring 反应器中的 Flux<T>
【发布时间】:2020-09-11 15:29:24
【问题描述】:

假设我有ProductSupplier,它允许通过 id 获取产品。但它有限制,每个请求只能加载一种产品。

public interface ProductSupplier {
    public Mono<Product> getById(Long productId);
}

现在我正在写 ProductService,我需要在其中按 id 获取产品列表

public interface ProductService {
    ProductSupplier supplier;

    public Mono<List<Product>> getByIds(Collection<Long> ids) {
        return ids.stream()
                  .map(supplier::getById)//Stream<Mono<Product>>
                  //how to get Flux<Product> here?
                  .collectList();
    }
}

【问题讨论】:

    标签: java reactive spring-reactive spring-reactor


    【解决方案1】:

    您可以直接使用通量而不是流:

    Flux<Product> flux = Flux
      .fromIterable(ids)
      .flatMap(supplier::getById);
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 2017-05-29
      • 2021-03-09
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2019-04-13
      相关资源
      最近更新 更多