title: Spring ResponseBodyEmitter doesn't set Content-Type header in a response
body: <p>I'm writing a kind of a wrapper around my request handlers to make them stream HTTP response. What I've got now is</p>
  
  <p><strong>Handler response wrapper:</strong></p>
  
  <pre><code> @Component
  public class ResponseBodyEmitterProcessor {
  
      public ResponseBodyEmitter process(Supplier&lt;?&gt; supplier) {
          ResponseBodyEmitter emitter = new ResponseBodyEmitter();
          Executors.newSingleThreadExecutor()
                  .execute(() -&gt; {
                      CompletableFuture&lt;?&gt; future = CompletableFuture.supplyAsync(supplier)
                              .thenAccept(result -&gt; {
                                  try {
                                      emitter.send(result, MediaType.APPLICATION_JSON_UTF8);
                                      emitter.complete();
                                  } catch (IOException e) {
                                      emitter.completeWithError(e);
                                  }
                              });
                      while (!future.isDone()) {
                          try {
                              emitter.send("", MediaType.APPLICATION_JSON_UTF8);
                              Thread.sleep(500);
                          } catch (IOException | InterruptedException e) {
                              emitter.completeWithError(e);
                          }
                      }
                  });
  
          return emitter;
      }
  }
  </code></pre>
  
  <p><strong>Controller:</strong> </p>
  
  <pre><code>@RestController
  @RequestMapping("/something")
  public class MyController extends AbstractController {
  
      @GetMapping(value = "/anything")
  public ResponseEntity&lt;ResponseBodyEmitter&gt; getAnything() {
      return ResponseEntity.ok()
              .contentType(MediaType.APPLICATION_JSON_UTF8)
              .body(process(() -&gt; {
                  //long operation
              }));
  }
  </code></pre>
  
  <p>What I'm doing is just send empty string every half a second to keep a request alive. It's required for some tool to not shut it down by timeout. The problem here that I don't see any <strong>Content-Type</strong> header in a response. There's nothing at all, despite I return ResponseEntity from my controller method as it's said in this thread: </p>
  
  <p><a href="https://github.com/spring-projects/spring-framework/issues/18518" rel="nofollow noreferrer">https://github.com/spring-projects/spring-framework/issues/18518</a></p>
  
  <p>Looks like only TEXT_HTML media type is acceptable for streaming. Isn't there a way to stream json at all? I even manually mapped my dtos to json string using objectMapper, but still no luck. Tried with APPLICATION_JSON and APPLICATION_STREAM_JSON - doesn't work. Tried in different browsers - the same result. </p>
  
  <p>I also manually set Content-Type header for ResponseEntity in the controller. Now there's the header in a response, but I'm not sure, if my data is actually streamed. In Chrome I can only see the result of my operation, not intermediate chars that I'm sending (changed them to "a" for test).</p>
  
  <p>I checked the timing of request processing for two options:</p>
  
  <p><strong>Without emitter (just usual controller handler)</strong>
  <a href="https://i.stack.imgur.com/9J8Q6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9J8Q6.png" alt="Without emitter"></a></p>
  
  <p><strong>With emitter</strong>
  <a href="https://i.stack.imgur.com/1tKhL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1tKhL.png" alt="With emitter"></a></p>
  
  <p>As I understand Waiting status means: "Waiting for the first byte to appear". Seems like with emitter the first byte appears much earlier - this looks like what I need. Can I consider it as a proof that my solution works?</p>
  
  <p>Maybe there's another way to do it in Spring? What I need is just to notify the browser that a request is still being processed by sending some useless data to it until the actual operation is done - then return the result.</p>
  
  <p>Any help would be really appreciated.</p>