Spring REST consumption results in HTTP Status 406 - Not Acceptable

ndrizza picture ndrizza · Jul 11, 2012 · Viewed 35.6k times · Source

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?

Answer

Tom van der Woerdt picture Tom van der Woerdt · Jul 11, 2012

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