Skip to content
Advertisement

Making multiple requests with Spring WebClient

So my goal is to use WebClient to make multiple concurrent requests, wait until they’re all completed, then combine the results. Here is what I have so far:

JavaScript

Basically my goal is to combine all the items from each of the feeds to create one unified feed. However, I am not quite sure what to do after the call to Flux::merge. Any suggestions would be appreciated.

Advertisement

Answer

Use .flatMap instead of .map / Flux.merge, like this:

JavaScript

Note that .flatMap is an asynchronous operation and will execute requests in parallel. There is an overloaded version that takes a concurrency argument if you want to limit concurrency.

Ordering is not guaranteed with .flatMap, and the resulting items might be interleaved. If you want more ordering guarantees, substitute .concatMap or .flatMapSequential.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement