Springmvc used the @responsebody report 406 error (1) _SPRINGMVC

Source: Internet
Author: User

Yesterday in writing a program, using @responsebody, to return a JSON format data, then found that the front desk has been reported 406 errors, the internet for a long time, some configuration

<mvc:annotation-driven>
		<mvc:message-converters>
			<bean id= "Fastjsonhttpmessageconverter" class= "Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" >
                <property name= " Supportedmediatypes ">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
		</mvc:message-converters>	
	</MVC: Annotation-driven>
Resolved (need to import Fastjson related packages)

Some configuration

<bean class= "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> < Bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > <property Name= "Messageconverters" > <list> <bean class= "Org.springframework.http.converter.s Tringhttpmessageconverter "> <property name=" Supportedmediatypes "> ;list> <value>text/html;
                        Charset=utf-8</value> <value>application/json;charset=UTF-8</value>  </list> </property> </bean> <bean class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > <property
                         Name= "Supportedmediatypes" > <list>   <value>text/html;
                        Charset=utf-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list&gt
        ; </property> </bean>
Resolved (need to import Jackon related packages)
However, I am here the two methods are invalid, temporarily do not know why, and then found an example on the Internet, download down, there is no above the two configurations, but added a @responsebody annotation can be used, looked at the jar package, Found that he used a version of 3.1.2, and I used the 3.2.5 version, I do not know whether this problem, and then debug, the cause of this problem has finally been found by me, as follows

Two versions of the Abstractmessageconvertermethodprocessor class have a method: Getacceptablemediatypes, this method gets the type of resource that can be received, this is where two versions are handled differently , so the results of the two versions are not the same, let's look at the implementation:

3.2.5 Version:

	Private list<mediatype> Getacceptablemediatypes (HttpServletRequest request) throws httpmediatypenotacceptableexception {
		list<mediatype> mediatypes = This.contentNegotiationManager.resolveMediaTypes (Request) (new servletwebrequest);
		Return Mediatypes.isempty ()? Collections.singletonlist (Mediatype.all): MediaTypes;
	}
3.1.2 Version:
	Private list<mediatype> getacceptablemediatypes (Httpinputmessage inputmessage) {
		try {
			list< mediatype> result = Inputmessage.getheaders (). getaccept ();
			Return Result.isempty ()? Collections.singletonlist (Mediatype.all): result;
		}
		catch (IllegalArgumentException ex) {
			if (logger.isdebugenabled ()) {
				Logger.debug ("Could not parse Accept Header: "+ ex.getmessage ());
			}
			return Collections.emptylist ();
		}
	
It is obvious that two methods are not the same, the key is the first sentence code, 3.1.2 version is directly to get the accept in the request header, the accept in the request header is */* (when using the AJAX request of jquery, the default is */*), so the JSON type can match to, So the conversion succeeds, and the 3.2.5 version is not getting the request header, tracking it all the way, and discovering that its key code implementations are as follows:

	Protected String Getmediatypekey (nativewebrequest webRequest) {
		HttpServletRequest servletrequest = Webrequest.getnativerequest (httpservletrequest.class);
		if (ServletRequest = = null) {
			Logger.warn ("an httpservletrequest are required to determine the media type key");
			return null;
		}
		String path = Urlpathhelper.getlookuppathforrequest (servletrequest);
		String filename = webutils.extractfullfilenamefromurlpath (path);
		String extension = stringutils.getfilenameextension (filename);
		Return (stringutils.hastext (extension))? Extension.tolowercase (locale.english): null;
	}
It is from the requested path to return the acceptable type, my SPRINGMVC match is the *.html suffix, so it finally returns the text/html type, obviously, can not match, so the final error

According to the above reasons, I changed the following code, and then successfully obtained the summer vacation:

1, the *.html suffix changed to/,406 error, but also successfully obtained the data

2, replace the 3.1.2 version of the package, HTML and/can get the data

This is my problem and solution, sent to share, and online said that the 2 kinds of configuration solution, I can not solve the problem here, temporarily do not know what the reason.



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.