I would like to transform/add the Mono from WebClient response into a Map with the input as a key
I am executing a batch of REST calls in parallel using WebClient but instead of returning the list of Users I would like to return a HashMap of ID as the key and the User returned from REST call as the value.
I don't want to block every individual call to get the value before I add to the HashMap.
Is there a way I can transform the result from WebClient into HashMap entry without impacting the parallel execution of the REST calls?
I tried doOnSuccess
callback for Mono but not sure if thats really the right way to do it.
Current Implementation
public List<<User> fetchUsers(List<Integer> ids) {
List<Mono<User>> userMonos = new ArrayList();
for (int id : ids) {
userMonos.add(webClient.get()
.uri("/otheruser/{id}", id)
.retrieve()
.bodyToMono(User.class));
}
List<User> list = Flux.merge(userMonos).collectList().block();
return list;
}
So the expected output is:
HashMap<Integer, User>()
I apologize if I wasn't able to express the expected result appropriately. Feel free to let me know if I need to add more detail or add more clarity to the question.
I would really appreciate some help with this. I am also trying to keep looking for a solution in the meantime.