SPRINGMVC Browser accepts JSON 406 error resolution

Source: Internet
Author: User

This error is really difficult to engage in, the first encounter there is really no good way to solve, the Internet to find a lot of information, one by one tried also can not be a good solution, finally through friends, colleagues work together, finally conquered. Leave notes for your reference:

1. My background test code is as follows:

@RequestMapping ("/json") public @ResponseBodyString json () {return ' this is JSON ';} @RequestMapping ("/json2") public @ResponseBodyList <User> Json2 () {list<user> users = new Arraylist<user > (); User U = new user (); U.setaccount ("AA"); Users.add (u); return users;}

When the browser accesses both methods, only JSON is correct, and Json2 is 406, which means that spring does not handle the problem of serializing the object into JSON, and finally I pass the log4j print log and clearly see that the format he returned is not in JSON format.

Since the format is wrong, the browser will certainly not be able to accept the data, the reason is here


2. I know the reason, so I searched the Internet. Some people on the internet say that MVC's XML does not add this configuration

<!--opening annotations--><mvc:annotation-driven/>

But my project is long overdue, not the answer.


Another is to configure the JSON template, feel this, and then learn to configure ..... Finally fell into the pit.

        <!--avoid IE when performing Ajax, return json appears download file--><bean id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "><property name=" Supportedmediatypes "><list><value>text/html;charset=utf-8</value></list></ property></bean><!--Start the annotation feature of spring MVC and complete the mapping of request and annotation Pojo--><beanclass= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/><!--JSON Converter-- </list></property></bean>

So configure a bit, start the project on the error, lack of packets, and then go online to find jar package, this has encountered version problem this is not easy, evening information did not say version of different to find the jar

Here I specifically explained

Spring 4.0 or later, generally find the version of Jackson 1.x

Spring 4.1 or above. We're looking for jackson2.x.

My project refers to the spring 4.1.2 version, the initial unclear situation, blindly according to the people on the Internet configuration, the reference to the Jackson version is relatively low, the problem is not resolved, but also attracted a lot of other problems, headache, death, die ...

Hey...... Unbearable to look back ... Okay, back to the theme.



The following is the correct configuration for the Spring 4.1.2 version

<----------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------>

4.1.2 's

<!--avoid IE when performing Ajax, return json appears download file--><bean id= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter "><property name=" Supportedmediatypes "><list><value>text/html;charset=utf-8</value></list></ property></bean><!--Start the annotation feature of spring MVC and complete the mapping of request and annotation Pojo--><beanclass= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/><!--JSON Converter-- </list></property></bean>

Introduction of three jackson2.4 versions

As follows:

Jackson-core-2.4.1.jar

Jackson-annotations-2.4.1.jar

Jackson-databind-2.4.1.jar

The problem is solved, not 406, the JSON data came out


Three jar I upload, for everyone to download ~

Address: http://download.csdn.net/detail/qq183293/8378947

Thank you for your support ~


SPRINGMVC Browser accepts JSON 406 error resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.