在弹簧反应器中将<Mono <T >>输送到通量<T>

问题描述

假设我有ProductSupplier,该ID允许按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();
    }
}

解决方法

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...