仅在第一次调用时调查HTTP 406错误代码

时间:2013-05-31 08:51:34

标签: spring http rest

我有一个使用Spring来暴露一些RestServices的服务器。我有~20个服务几乎总是相同的东西:获取,列表,保存......

在所有这些服务中,我有一个请求与其他服务不同:

@RequestMapping(value = {"/{id}"}, method = RequestMethod.GET)
@ResponseBody
public Line get(@PathVariable(value = "id") int id) {
  return lineService.getById(id);
}

问题:

  • 当我第一次重启服务器并调用此特定请求时,我收到406错误代码。

  • 之后,所有其他调用都以成功结束,使用HTTP 200代码返回格式良好的JSON等。

请求的Accept Headers包含“application / 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

在第二次通话中,我可以看到这一点(差异从第2行开始):

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

所以,在第一种情况下,我在尝试将对象转换为JSON时得到一些HttpMediaTypeNotAcceptableException。

4 个答案:

答案 0 :(得分:1)

我首先会点击Fiddler这样的内容,看看实际通过线路的是什么位。

答案 1 :(得分:1)

你说,Accept Headers包含application/json。我会通过发送请求开始调查,而不指定接受的内容类型。当服务器响应不受特定内容类型约束时,应该有一些相关的线索。如果在处理请求期间遇到任何错误,Web服务器通常会返回格式为HTML的错误页面。在这种情况下,内容类型当然是text / html,因此真正的问题会因406错误而被混淆。

答案 2 :(得分:0)

您是否尝试在控制器方法的第一行设置断点,即return lineService.getById(id);

我怀疑你会遇到那条线,但是如果你试图进入"那么会出现一些初始错误。使用调试器的那个方法。如果我的假设是正确的,某处会抛出一个异常,导致一个带有堆栈跟踪的html页面。接下来,您将看到406出现,因为html页面具有媒体类型application/html或类似的,无法转换为application/json

答案 3 :(得分:0)

鉴于您只在重新启动服务器后才看到此错误,让我觉得这可能与延迟类加载有关。

尝试使用在服务器启动时初始化的servlet(即load-on-startup值为1),在其init方法中 - 指向呈现com.mycompany.myproject.dto.datagrid.LoadResult所涉及的所有类去json?

请记住,您正在尝试确保所有涉及的类在请求进入之前由ClassLoader加载。