我有一臺服務器使用Spring來公開一些RestServices。我有〜20個服務,做幾乎總是同樣的事情:獲取,列出,保存...僅在第一次調用時調查HTTP 406錯誤代碼
在所有這些服務,我有一個請求,不表現得和別人一樣:
@RequestMapping(value = {"/{id}"}, method = RequestMethod.GET)
@ResponseBody
public Line get(@PathVariable(value = "id") int id) {
return lineService.getById(id);
}
問題:
當我重新啓動服務器並第一次調用此特定請求時,我得到一個406錯誤代碼。
在此之後,所有其他呼叫結束成功,返回合式JSON與HTTP 200代碼等
請求的接受頭包含「應用程序/ JSON」。
我嘗試添加這對requestMapping:
produces="application/json"
看來某處,服務器後適應內容/類型的效應初探第一失敗,但我可以肯定不明白的地方。
我嘗試使用tomcat和jetty作爲後端服務器,並且兩者都出現錯誤。
任何想法?
編輯:
經過進一步調查,我發現在Tomcat的日誌這種差異。在第一次調用(HTTP 406),我可以看到這一點:
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
在第二個電話,我可以看到這個(差別在第二行開始):
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 [[email protected]18949ad1] as "application/json;charset=UTF-8" using [org.springf[email protected]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
因此,在第一大概,當我試圖將對象轉換爲JSON時,我得到了一些HttpMediaTypeNotAcceptableException。
爲什麼如果示例中有Spring註釋(而不是JAX-RS),那麼您是否使用了Jersey?對這些技術會有一些誤解嗎? –
經過對maven depasis的檢查之後,這是一個錯誤:我們不使用Jersey。我編輯了這個問題,謝謝。 –
我認爲406是關於請求和響應的:當響應的Content-Type與請求的「Accept Header」不對應時,它會發生,或者我錯過了什麼?對於Spring處理程序映射,團隊和我正在接近這一點,仍在調查。 –