title: How to properly drain / release response body from WebFlux Client?
body: <p>I am using WebFlux HTTP Client from Spring 5 reactive stack to access external REST service. I want to process responses based on HTTP status:</p>
  
  <ol>
  <li><p>If status is 2xx I want to return <code>Mono</code> with deserizlized response body.</p></li>
  <li><p>If status is 404 I want to drop response body and immediately return empty <code>Mono</code>.</p></li>
  <li><p>For any other status I want to drop response body and return error Mono with <code>MyBusinessException</code>.</p></li>
  </ol>
  
  <p>My code looks like this:</p>
  
  <pre><code>webClient.get()
      .uri("/search")
      .syncBody(request)
      .exchange()
      .flatMap { response -&gt;
          when {
              response.statusCode().is2xxSuccessful -&gt; response.bodyToMono(MyResponse::class.java)
              response.statusCode() == NOT_FOUND -&gt; Mono.empty()
              else -&gt; MyBusinessException().toMono&lt;MyResponse&gt;()
          }
       }
  </code></pre>
  
  <p>I don't want to waste time to receive and process response body when I don't need it. JavaDoc for <code>exchange()</code> method states</p>
  
  <blockquote>
    <p>You must always use one of the body or entity methods of the response to ensure resources are released.</p>
  </blockquote>
  
  <p>How exactly should I do this if I want to drain response body and return result immediately?</p>