Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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>

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top