更新时间:2022-02-02 05:28:49
这里的主要问题是路径 "/test.htm"
在检查值之前将首先使用内容协商Accept
标头.对于像 *.htm
这样的扩展,Spring 将使用 org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy
并解析返回的可接受媒体类型是 text/html
与 MappingJacksonHttpMessageConverter
产生的不匹配,即.application/json
因此返回 406.
The main issue here is that the path "/test.htm"
is going to use content negotiation first before checking the value of an Accept
header. With an extension like *.htm
, Spring will use a org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy
and resolve that the acceptable media type to return is text/html
which does not match what MappingJacksonHttpMessageConverter
produces, ie. application/json
and therefore a 406 is returned.
简单的解决方案是将路径更改为类似 /test
的内容,其中基于路径的内容协商不会为响应解析任何内容类型.相反,基于标头的不同 ContentNegotiationStrategy
将解析 Accept
标头的值.
The simple solution is to change the path to something like /test
, in which content negotiation based on the path won't resolve any content type for the response. Instead, a different ContentNegotiationStrategy
based on headers will resolve the value of the Accept
header.
复杂的解决方案是更改在处理您的 @ResponseBody
的 RequestResponseBodyMethodProcessor
中注册的 ContentNegotiationStrategy
对象的顺序.
The complicated solution is to change the order of the ContentNegotiationStrategy
objects registered with the RequestResponseBodyMethodProcessor
which handles your @ResponseBody
.