问题描述
假设我有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);