title: Spring controller: Content-Type for StreamingResponseBody
body: <p>In search for the least amount of code to serve a <code>BufferedImage</code> (painted in a service-class) from a spring-mvc <code>Controller</code>, I came up with the following:</p>
  
  <pre><code>@GetMapping(value = "/image", produces = "image/png")
  public StreamingResponseBody image() {
      BufferedImage canvas = service.createImage();
      return outputStream -&gt; ImageIO.write(canvas, "png", outputStream);
  }
  </code></pre>
  
  <p>So far, so good - using the image in HTML via <code>&lt;img src="/image"&gt;</code> works fine.</p>
  
  <p>However, spring does not send any <code>Content-Type</code> header at all. Together with serving <code>X-Content-Type-Options: nosniff</code> in the response, this leads to garbage in the browser window when the image URL is opened directly.</p>
  
  <p>How do I provide a content-type header in the most spring-friendly way (i.e. not using <code>HttpServletResponse</code> directly)?</p>