Spring: Getting HTTP 406 from a REST controller
You may get a HTTP 406 Not Acceptable
error while trying to return Java objects from a REST controller.
The server is not able to handle your request because the HTTP header “Accept” does not match with any of the content types it can handle.
In fact, the server might be unable to deliver the response in whatever content-type, some piece of configuration is missing in your project.
Example
Let’s declare a “GET /dogs” method in your Spring @Controller:
Using Spring you have to declare a mapper in order to tell Spring how to convert Java POJO to whatever content type. Let’s say you want to convert your POJOs to JSON, so you have to use a JSON Mapper.
Maven dependencies to add
Spring version = 3.1.2.RELEASE
Spring version >= 3.1.2 RELEASE
Just redeploy your webapp and you’ll be able to get your Java POJO as a JSON object.
Side notes:
- You can use the class annotation @RestController to avoid adding method annotation @ResponseBody
- HTTP 406 RFC