Bohdan Petrenko
Bohdan Petrenko

Reputation: 1155

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();
    }
}

Upvotes: 1

Views: 1319

Answers (1)

sp00m
sp00m

Reputation: 48807

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

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

Upvotes: 1

Related Questions