springresthttp-status-code-406

Spring REST consumption results in HTTP Status 406 - Not Acceptable


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?


Solution

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