简体   繁体   中英

Stream<Mono<T>> to Flux<T> in spring reactor

Lets say I have ProductSupplier which allow to get product by id. But it has restrictions and per one request you can load only one product.

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

Now I'm writing ProductService in which I need to fetch a list of products by 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();
    }
}

You could work on a flux directly instead of a stream:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM