Home >Java >javaTutorial >**Why am I getting a 406 (Not Acceptable) error when retrieving JSON data in my Spring MVC application?**
Spring JSON Request Error: 406 Not Acceptable
In a Spring MVC application, while using AJAX to retrieve JSON data, a "406 (Not Acceptable)" error can occur. This indicates that the server cannot generate a response that meets the specified content characteristics, as defined by the request header.
To resolve this issue, ensure that your Spring configuration is correctly set up and that the required libraries are included in your classpath. Specifically, check the following:
1. HTTP Message Converter Registration:
Make sure that you have configured HTTP Message Converters for JSON. This is typically done automatically when using
2. Third-Party Libraries:
Verify that you have the appropriate Jackson libraries in your classpath. Specifically, you will need:
3. Controller Configuration:
Remove the headers="Accept=*/*" directive from your controller method. This directive is unnecessary and can interfere with proper content negotiation.
Example:
<code class="java">@RequestMapping(value="/getTemperature/{id}", method = RequestMethod.GET) @ResponseBody public Weather getTemparature(@PathVariable("id") Integer id){ Weather weather = weatherService.getCurrentWeather(id); return weather; }</code>
The above is the detailed content of **Why am I getting a 406 (Not Acceptable) error when retrieving JSON data in my Spring MVC application?**. For more information, please follow other related articles on the PHP Chinese website!