title: Investigate HTTP 406 Error code only on first call
body: <p>I have a server which uses Spring to expose some RestServices. I have ~20 services which do almost always the same things : get, list, save...</p>
  
  <p>Among all those services, I have ONE request that does not behave like the others : </p>
  
  <pre><code>@RequestMapping(value = {"/{id}"}, method = RequestMethod.GET)
  @ResponseBody
  public Line get(@PathVariable(value = "id") int id) {
    return lineService.getById(id);
  }
  </code></pre>
  
  <p>Problem :</p>
  
  <ul>
  <li><p>When I restart the server and call this particular request for the very first time, I get a 406 Error Code.</p></li>
  <li><p>After that, all other calls end in success, returning well-formed JSON with HTTP 200 code etc.</p></li>
  </ul>
  
  <p>The Accept Headers of the request contains "application/json".</p>
  
  <p>I tried to add this on the requestMapping : </p>
  
  <pre><code>produces="application/json"
  </code></pre>
  
  <p>It seems that somewhere, the server adapts the content/type of the reponse after the first fail but I definitely cannot understand where.</p>
  
  <p>I tried with tomcat and jetty as backend server and I have the error on both.</p>
  
  <p>Any ideas?</p>
  
  <p>EDIT : </p>
  
  <p>After further investigation, I detected this difference in tomcat logs. On the first call (HTTP 406), I can see this :</p>
  
  <pre><code>09:33:58.637 [http-bio-9090-exec-3] DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
  09:33:58.637 [http-bio-9090-exec-3] DEBUG o.s.w.a.FixedContentNegotiationStrategy - Requested media types is application/json (based on default MediaType)
  09:33:58.654 [http-bio-9090-exec-3] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.mycompany.myproject.dto.MyClass com.mycompany.myproject.controller.patrimoine.LigneMobileController.get(int)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public com.mycompany.myproject.dto.MyClass com.mycompany.myproject.controller.patrimoine.LigneMobileController.get(int)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.mycompany.myproject.dto.MyClass com.mycompany.myproject.controller.patrimoine.LigneMobileController.get(int)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'springServlet': assuming HandlerAdapter completed request handling
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
  09:33:58.657 [http-bio-9090-exec-3] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
  </code></pre>
  
  <p>On the second call, I can see this (differences start at 2nd line): </p>
  
  <pre><code>09:50:52.384 [http-bio-9090-exec-7] DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
  09:50:52.385 [http-bio-9090-exec-7] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Written [com.mycompany.myproject.dto.patrimoine.mobile.ligne.LigneMobile@18949ad1] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@32a64bcd]
  09:50:52.385 [http-bio-9090-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'springServlet': assuming HandlerAdapter completed request handling
  09:50:52.385 [http-bio-9090-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
  09:50:52.385 [http-bio-9090-exec-7] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
  09:50:52.385 [http-bio-9090-exec-7] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
  </code></pre>
  
  <p>So, in the first case, I get some HttpMediaTypeNotAcceptableException when trying to convert the object to JSON.</p>