gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:34:10 30 4
gpt4 key购买 nike

我有一个使用 Spring 公开一些 RestServices 的服务器。我有大约 20 种服务,它们几乎总是做同样的事情:获取、列出、保存...

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

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

问题:

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

  • 之后,所有其他调用都成功结束,返回格式正确的 JSON 和 HTTP 200 代码等。

请求的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

在第二次调用时,我可以看到这一点(差异从第二行开始):

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。

最佳答案

我会先启动类似 Fiddler 的东西以查看实际上有哪些位通过网络传输。

关于spring - 仅在第一次调用时调查 HTTP 406 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16853410/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com