title: How to best get a byte array from a ClientResponse from Spring WebClient?
body: <p>I'm trying out the new <code>WebClient</code> from Spring 5 (5.0.0.RC2) in a codebase that uses reactive programming and I've had success mapping the JSON response from an endpoint to a DTO in my app, which works very nice:</p>
  
  <pre><code>WebClient client = WebClient.create(baseURI);
  Mono&lt;DTO&gt; dto = client.get()
          .uri(uri)
          .accept(MediaType.APPLICATION_JSON)
          .exchange()
          .flatMap(response -&gt; response.bodyToMono(DTO.class));
  </code></pre>
  
  <p>However, now I'm trying to the response body from an endpoint which uses Protocol Buffers (binary data served as <code>application/octet-stream</code>), so I'd like to get the raw bytes from the response, which I'll then map to an object myself.</p>
  
  <p>I got it to work like this using <code>Bytes</code> from Google Guava:</p>
  
  <pre><code>Mono&lt;byte[]&gt; bytes = client.get()
          .uri(uri)
          .accept(MediaType.APPLICATION_OCTET_STREAM)
          .exchange()
          .flatMapMany(response -&gt; response.body(BodyExtractors.toDataBuffers()))
          .map(dataBuffer -&gt; {
              ByteBuffer byteBuffer = dataBuffer.asByteBuffer();
              byte[] byteArray = new byte[byteBuffer.remaining()];
              byteBuffer.get(byteArray, 0, bytes.length);
              return byteArray;
          })
          .reduce(Bytes::concat)
  </code></pre>
  
  <p>This works, but is there an easier, more elegant way to get these bytes?</p>