title: Reactive Programming: Spring WebFlux: How to build a chain of micro-service calls?
body: <p><code>Spring Boot</code> Application:</p>
  
  <p>a <code>@RestController</code> receives the following payload:</p>
  
  <pre><code>{
    "cartoon": "The Little Mermaid",
    "characterNames": ["Ariel", "Prince Eric", "Sebastian", "Flounder"]
  }
  </code></pre>
  
  <p>I need to process it in the following way:</p>
  
  <ol>
  <li>Get the unique Id for each character name: make an HTTP call to "cartoon-characters" microservice, that returns ids by names</li>
  <li><p>Transform data received by the controller: 
  replace character names with appropriate ids that were received on the previous step from "cartoon-characters" microservice.
     <code>{
      "cartoon": "The Little Mermaid",
      "characterIds": [1, 2, 3, 4]
      }</code></p></li>
  <li><p>Send an HTTP POST request to "cartoon-db" microservice with transformed data.</p></li>
  <li>Map the response from "cartoon-db" to the internal representation that is the controller return value.</li>
  </ol>
  
  <p>The problem that I got:</p>
  
  <p>I need to implement all these steps using the paradigm of <code>Reactive Programming</code> (non-blocking\async processing) with <code>Spring WebFlux</code> (<code>Mono</code>|<code>Flux</code>) and <code>Spring Reactive WebClient</code> - but I have zero experience with that stack, trying to read about it as much as I can, plus googling a lot but still, have a bunch of unanswered questions, for example:</p>
  
  <p><strong>Q1</strong>. I have already configured reactive webClient that sends a request to "cartoon-characters" microservice:</p>
  
  <pre><code>      public Mono&lt;Integer&gt; getCartoonCharacterIdbyName(String characterName) {
      return WebClient.builder().baseUrl("http://cartoon-characters").build()
          .get()
          .uri("/character/{characterName}", characterName)
          .retrieve()
          .bodyToMono(Integer.class);
    }
  </code></pre>
  
  <p>As you may see, I have got a list of cartoon character names and for each of them I need to call <code>getCartoonCharacterIdbyName(String name)</code> method, I am not sure that the right option to call it in series, believe the right option: parallel execution.</p>
  
  <p>Wrote the following method:</p>
  
  <pre><code>  public List&lt;Integer&gt; getCartoonCharacterIds(List&lt;String&gt; names) {
  Flux&lt;Integer&gt; flux = Flux.fromStream(names.stream())
      .flatMap(this::getCartoonCharacterIdbyName);
  
  return StreamSupport.stream(flux.toIterable().spliterator(), false)
      .collect(Collectors.toList());
  </code></pre>
  
  <p>}</p>
  
  <p>but I have doubts, that this code does parallel <code>WebClient</code> execution and also, code calls <code>flux.toIterable()</code> that block the thread, so with this implementation I lost non-blocking mechanism.</p>
  
  <p>Are my assumptions correct?</p>
  
  <p>How do I need to rewrite it to having parallelism and non-blocking?</p>
  
  <p><strong>Q2.</strong>
  Is it technically possible to transform input data received by the controller (I mean replace names with ids) in reactive style: when we operate with <code>Flux&lt;Integer&gt;</code> characterIds, but not with the <code>List&lt;Integer&gt;</code> of characterIds?</p>
  
  <p><strong>Q3.</strong> Is it potentially possible to get not just transformed Data object, but Mono&lt;> after <strong>step 2</strong> that can be consumed by another WebClient in <strong>Step 3</strong>?</p>