spring-webfluxspring-webclientspring-reactivespring5

webClient response using bodyToFlux method merging all the received response instead separating it out


  1. I am using ParallelFlux for running many task.

  2. but when i am receiving webClient response using bodyToFlux method its merging all the output response instead of getting one by one.

  3. i want the output should be one by one not single string, is there any other method instead of bodyToFLux need to use.

    request method:

        Flux<String> responsePost = webClient.build()
                                //removed get,url and retrieve here                             
                                .bodyToFlux(String.class);
    
                    responsePost.subscribe(s -> {
                        //display response
                        });
    

    response method:

        public ParallelFlux<String> convertListToMap() {
                  //created list of string str
    
                return Flux.fromIterable(str)
                        .parallel(3)
                        .runOn(Schedulers.parallel())
                        .map( s -> {
                    //some logic here
                });
           }
    

    output:

        parallel fulx reponse: springwebfluxparellelExample
    

Solution

    1. Here instead of returning ParallelFlux create an ResponseBody class with variable you want to return, assign your response to variable inside ResponseBody and return it to caller of API function.

      public ParallelFlux<ResponseBody> convertListToMap() {
                    //created list of string str
      
                  return Flux.fromIterable(str)
                          .parallel(3)
                          .runOn(Schedulers.parallel())
                          .map( s -> {
                      //some logic here
                  });
             }
      
      
      
       Flux<String> responsePost = webClient.build()
                                  //removed get,url and retrieve here                             
                                  .bodyToFlux(ResponseBody.class);