title: How to avoid HttpMediaTypeNotAcceptableException with Spring SseEmitter on timeout
body: <p>I get the error <code>HttpMediaTypeNotAcceptableException</code> when the SseEmitter has timed out.</p>
  
  <p>When searching in SO for this error code, I find only answers relating to registering a converter for the <code>ResponseBody</code>. But I can't see how this could be the same problem I am facing, since the content type is <code>text/event-stream</code> and is handled by Spring automatically.</p>
  
  <p>However, the communication seems to flow good otherwise; the messages are being sent properly to the client.</p>
  
  <pre><code>@RequestMapping(path = "/channel")
  @CrossOrigin
  public SseEmitter channel() throws IOException {
  
      SseEmitter emitter = new SseEmitter();
  
      // Storing the emitter
      // ...
      player.setEmitter(emitter);
  
      return emitter;
  }
  </code></pre>
  
  <p>In the player, I do this:</p>
  
  <pre><code>public void setEmitter(SseEmitter emitter) {
      if (this.emitter != null) {
          // Old emitter must be completed for user
          this.emitter.complete();
      }
  
      this.emitter = emitter;
      this.emitter.onTimeout(() -&gt; {
          if (this.emitter != null) this.emitter.complete();
      });
      this.emitter.onCompletion(() -&gt; this.emitter = null);
  }
  
  public void sendMessage(String message) {
      if (this.emitter != null) {
          emitter.send(message);
      }
  }
  </code></pre>
  
  <p>(By the way, is this a good way of handling the emitter; i.e. the registration of onTimeout and onCompletion? The idea is that a new emitter is created on every reconnect and that means the old one needs to be replaced.)</p>
  
  <p>The messages sent are a data structure serialized into JSON text. On the JavaScript client it is correctly deserialized into a JSON object. I have not specified the JSON content anywhere, as I think from the SseEmitter/SSE perspective, it is just text.</p>
  
  <p>During the time the connection is established, everything works fine. At every timeout, the client reconnects as it should.
  However I seem to always get the following exception in connection to the timeout and reconnect procedure:</p>
  
  <pre><code>org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
      at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:191) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) ~[javax.servlet-api-3.1.0.jar:3.1.0]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[javax.servlet-api-3.1.0.jar:3.1.0]
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812) ~[jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) ~[jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595) ~[jetty-security-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1127) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) ~[jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.Server.handleAsync(Server.java:549) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:348) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:262) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
      at java.lang.Thread.run(Thread.java:745) [na:na]
  </code></pre>
  
  <p>I am using Spring Boot with Jetty. I also get this error, but I think it is a consequence of the first:</p>
  
  <pre><code>java.lang.IllegalStateException: Committed
      at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1243) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.Response.sendError(Response.java:567) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.Response.sendError(Response.java:544) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:167) ~[javax.servlet-api-3.1.0.jar:3.1.0]
      at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:167) ~[javax.servlet-api-3.1.0.jar:3.1.0]
      at org.springframework.security.web.context.OnCommittedResponseWrapper.sendError(OnCommittedResponseWrapper.java:95) ~[spring-security-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
      at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMediaTypeNotAcceptable(DefaultHandlerExceptionResolver.java:257) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:121) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:137) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:74) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1185) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1022) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [javax.servlet-api-3.1.0.jar:3.1.0]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812) [jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) [jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595) [jetty-security-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1127) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.Server.handleAsync(Server.java:549) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:348) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:262) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
      at java.lang.Thread.run(Thread.java:745) [na:na]
  </code></pre>