Spring REST consumption results in HTTP Status 406 - Not Acceptable

做~自己de王妃 提交于 2019-12-10 19:59:18

问题


I get this error when i try to consume a 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?


回答1:


You're sending an Accept= header instead of an Accept: header.




回答2:


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".

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.




回答3:


add this to spring mvc dispatcher:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</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.MappingJacksonHttpMessageConverter"/>
        </list>
    </property>
</bean>



回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/11429808/spring-rest-consumption-results-in-http-status-406-not-acceptable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!