title: Return a stream with Spring MVC's ResponseEntity
body: <p>I have a Spring MVC method which returns a <code>ResponseEntity</code>. Depending on the specific data retrieved, it <em>sometimes</em> needs to return a stream of data to the user. Other times it will return something other than a stream, and sometimes a redirect. I most definitely want this to be a stream and not a byte array since it can be large.</p>
  
  <p>Currently, I return the stream using the following snippet of code:</p>
  
  <pre><code>HttpHeaders httpHeaders = createHttpHeaders();
  IOUtils.copy(inputStream, httpServletResponse.getOutputStream());
  
  return new ResponseEntity(httpHeaders, HttpStatus.OK);
  </code></pre>
  
  <p>Unfortunately, this does not allow the Spring <code>HttpHeaders</code> data to actually populate the HTTP Headers in the response. This makes sense since my code writes to the <code>OutputStream</code> before Spring receives the <code>ResponseEntity</code>.</p>
  
  <p>It would be very nice to somehow return a <code>ResponseEntity</code> with an <code>InputStream</code> an let Spring handle it. It also would parallel the other paths of my function where I can successfully return a <code>ResponseEntity</code>. Is there anyway I can accomplish this with Spring?</p>
  
  <hr>
  
  <p>Also, I did try returning the <code>InputStream</code> in the <code>ResponseEntity</code> just to see if Spring would accept it.</p>
  
  <pre><code>return new ResponseEntity(inputStream, httpHeaders, HttpStatus.OK);
  </code></pre>
  
  <p>But it throws this exception:</p>
  
  <pre><code>org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  </code></pre>
  
  <hr>
  
  <p>I can get my function to work by setting everything on the <code>HttpServletResponse</code> directly, but I would like to do this only with Spring.</p>