3

I want to retrieve all pages from a third-party resource. To do that I wrote this:

final WebClient webClient = WebClient.builder()
     .baseUrl("http://resource.com")
     .build();
Flux.fromStream(IntStream.iterate(0, i -> i + 1).boxed())
     .flatMap(i -> webClient.get()
          .uri("/data?page={page}", i)
          .retrieve()
          .bodyToMono(Page.class))
     .takeWhile(Page::isMoreAvailable)
     .flatMapIterable(Page::getData)

but it doesn't work properly. flatMap is called multiple times and it performs multiple requests with different pages before takeWhile retrieves the first response.

If I change it to this:

.map(i -> webClient.get()
        .uri("/data?page={page}", i)
        .retrieve()
        .bodyToMono(Page.class)
        .block())

it works well.

So, how can I achieve this with a flatMap?

Slava
  • 311
  • 3
  • 9

1 Answers1

1

ok, so flatmap processing concurrent

.flatMap(i -> webClient.get()
   .uri("/data?page={page}", i)
   .retrieve()
   .bodyToMono(Page.class), 1)

did the trick.

Slava
  • 311
  • 3
  • 9