SpringMVC browser accepts the solution of 406 error in json format, springmvcjson

Source: Internet
Author: User

SpringMVC browser accepts the solution of 406 error in json format, springmvcjson

This error is really hard to solve. There is really no good solution for the first meeting. I found a lot of information on the Internet and tried it one by one. Finally, my friends and colleagues worked together to solve the problem, 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 these two methods, only json is correct, and json2 returns 406, indicating that spring has not processed the problem of Object serialization into json. Finally, I printed the logs through log4j, clearly, the returned format is not in json format.

Since the format is wrong, the browser certainly cannot accept the data, the reason is that


2. I found the cause and searched the internet. Someone on the Internet said that this configuration was not added to the mvc xml.

<! -- Enable annotation --> <mvc: annotation-driven/>

But I already have a project, and it is not the answer.


The other is to configure the json template. I think this is reliable, so I learned how to configure it... and finally fell into the trap.

<! -- When IE executes AJAX, the downloaded file is returned in JSON --> <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 function of Spring MVC to map requests and annotation POJO --> <beanclass = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "> <property name =" messageConverters "> <list> <ref bean =" mappingJacksonHttpMessageConverter "/> <! -- Json converter --> </list> </property> </bean>

After the configuration, an error is reported when the project is started. Due to the shortage of packages, it is not easy to find the jar package online, jar cannot be found if the version is not specified at night.

Here I will explain it specially

Spring 4.0 or later, generally look for the jackson 1.x version.

For spring 4.1 or later, you need to find jackson2.x or later.

My project references the spring 4.1.2 version, which is not clear at the beginning and is blindly configured by people on the Internet. The reference to the jackson version is relatively low and the problem is not solved in the end, there are also a bunch of other problems, and the headache is dying .....

Well, back to the topic



The following are the correct configurations of spring 4.1.2.

<Commandid>

4.1.2

<! -- When IE executes AJAX, the downloaded file is returned in JSON --> <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 function of Spring MVC to map requests and annotation POJO --> <beanclass = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "> <property name =" messageConverters "> <list> <ref bean =" mappingJacksonHttpMessageConverter "/> <! -- Json converter --> </list> </property> </bean>

Introduce three versions of jackson2.4

As follows:

Jackson-core-2.4.1.jar

Jackson-annotations-2.4.1.jar

Jackson-databind-2.4.1.jar

The problem is solved. If 406 is not reported, the json data is returned.


Upload the Three jar files for you to download ~

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

Thank you for your support ~


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.