Home > Java > javaTutorial > body text

**Why am I getting a 406 (Not Acceptable) error when retrieving JSON data in my Spring MVC application?**

Patricia Arquette
Release: 2024-10-25 10:26:02
Original
283 people have browsed it

**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 in your Spring context configuration.

2. Third-Party Libraries:
Verify that you have the appropriate Jackson libraries in your classpath. Specifically, you will need:

  • jackson-core-asl-1.x.x.jar
  • jackson-mapper-asl-1.x.x.jar

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!