0

Using Spring boot Web client to request JSON from a REST API, the response body from the server is stored in a simple Map. The request to the server contains an ID, but this isn't included in the response, but is required to interpret the response.

I would want to include the id in the result of the parallel call, so instead of instead of List<Map> data should it be Map<UUID,Map> data or similar. So the response from the server is paired with the id that was used to get the response.

        List<Map> data = Flux.fromIterable(ids)
                .parallel()
                .runOn(Schedulers.elastic())
                .flatMap(this::callAPI).sequential().collectList().block();
    private Mono<Map> callAPI(UUID id) {
        return client.get().uri(uriBuilder -> uriBuilder
                .path("/{id}/")
                .build(id))
                .retrieve().bodyToMono(Map.class);
    }
Linor
  • 55
  • 1
  • 5
  • why dont you try `collectMap` instead of `collectList` – Toerktumlare May 02 '20 at 01:03
  • @ThomasAndolf How would I supply the `UUID id` into the `collectMap`? – Linor May 02 '20 at 16:59
  • By not returning a `Mono` from your `callApi` function. Return a `Map` and the `Id` in wrapper object that you can later use to remap to your wanted structure `.bodyToMono(Map.class).flatMap(map -> { return Mono.just(new Response(id, map)); });` – Toerktumlare May 02 '20 at 19:46

1 Answers1

0

From @ThomasAndolf comment

By not returning a Mono<Map> from your callApi function. Return a Map and the Id in wrapper object that you can later use to remap to your wanted structure .bodyToMono(Map.class).flatMap(map -> { return Mono.just(new Response(id, map)); }); – Thomas Andolf 22 hours ago

Linor
  • 55
  • 1
  • 5