Spring REST 消耗导致 HTTP 状态 406 - 不可接受
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11429808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring REST consumption results in HTTP Status 406 - Not Acceptable
提问by ndrizza
I get this error when i try to consume a REST API:
当我尝试使用 REST API 时出现此错误:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable
Here's the client code that gets executed:
这是执行的客户端代码:
public static void main(String[] args) {
Car c = getCarById(4);
System.out.println(c);
}
public static @ResponseBody Car getCarById(int id){
return new RestTemplate().getForObject("http://localhost:8080/rest/cars/{id}", Car.class, id);
}
Here's the code of the Controller which maps the request:
这是映射请求的控制器的代码:
@RequestMapping(value="/cars/{id}", method=RequestMethod.GET, headers = {"Accept=text/html,application/xhtml+xml,application/xml"}, produces="application/xml")
public @ResponseBody Car getCarById(@PathVariable("id") int id){
return carService.getCarById(id);
}
Why is this error (406-Not Acceptable) happening although the mappers should take care of mapping to the correct types?
尽管映射器应该负责映射到正确的类型,但为什么会发生此错误(406-Not Acceptable)?
采纳答案by Tom van der Woerdt
You're sending an Accept=header instead of an Accept:header.
您发送的是Accept=标头而不是Accept:标头。
回答by koljaTM
I got this answer when I had a wrong Accept: header in my request. I was trying to request an image/jpeg, but my request contained "Accept: application/json".
当我的请求中有错误的 Accept: 标头时,我得到了这个答案。我试图请求一个图像/jpeg,但我的请求包含“接受:应用程序/json”。
The solution was to use the correct entity class to query for (I was querying for Object just to see what would come), in my case Resource.class.
解决方案是使用正确的实体类来查询(我查询 Object 只是为了看看会发生什么),在我的例子中是 Resource.class。
回答by Yura
add this to spring mvc dispatcher:
将此添加到 spring mvc 调度程序:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- JSON format support for Exception -->
<bean id="methodHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"/>
</list>
</property>
</bean>
回答by Pedro Madrid
In my case, I fixed this not on the server side but in the client side. I was using Postman and was getting the 406 error. But using a browser was processing the request just fine. So I looked at the request headers in the browser and added the Accept header in Postman, like this: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
就我而言,我不是在服务器端而是在客户端修复了这个问题。我正在使用 Postman 并收到 406 错误。但是使用浏览器可以很好地处理请求。于是我在浏览器中查看了请求头,并在 Postman 中添加了 Accept 头,如下所示:Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
回答by wakedeer
In addition, you can fix add "Accept", "/"
此外,您可以修复添加“接受”、“ /”
val headers = HttpHeaders()
headers.add("Accept", "*/*")
val httpEntity = HttpEntity("parameters", headers)
restTemplate.exchange(....)
restTemplate.exchange("http://localhost:" + serverPort + "/product/1",
HttpMethod.GET,
httpEntity,
String.javaClass)
Sorry, it's Kotlin
抱歉,这是 Kotlin
回答by leon cio
I have had the same problem and finally it was a library problem. If you don't use maven you have to be sure you have include json core library and all its dependencies. If your method has input parameters loaded in json format and you don't have this libraries you will have a 415 error. I think the two error has the same origin: incomplete libraries.
我遇到了同样的问题,最后是图书馆问题。如果您不使用 maven,则必须确保包含 json 核心库及其所有依赖项。如果您的方法以 json 格式加载了输入参数,而您没有此库,则会出现 415 错误。我认为这两个错误有相同的起源:不完整的库。

