title: How to stream binary data in a response body in Spring WebFlux
body: <p>I am making a project using Spring WebFlux.</p>
  
  <p>In the past I had used <code>StreamingResponseBody</code> for streaming responses back to the client, but I can't find the equivalent in WebFlux.</p>
  
  <p>Example:</p>
  
  <pre><code>import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
  
  @GetMapping("/video")
  public StreamingResponseBody stream() {
      InputStream videoStream = ...
      StreamingResponseBody res = (os) -&gt; { IOUtils.copy(videoStream, os); }
      return res;
  }
  </code></pre>
  
  <p>Is there an equivalent of <code>StreamingResponseBody</code> for WebFlux? or, should I import the traditional Spring MVC and mix them?</p>
  
  <p><strong>Edit</strong>: So far I am solving it by accessing the <code>ServerHttpResponse</code> (example below). But I am still wondering about better solutions.</p>
  
  <pre><code>@GetMapping("/video")
  fun stream2(response: ServerHttpResponse): Mono&lt;Void&gt; {
      val factory = response.bufferFactory()
      val publisher = videoStream
              .observeVideoParts()
              .map { factory.wrap(it.bytes) }
      return response.writeWith(publisher)
  }
  </code></pre>