Spring 3.2.x returns JSON data through the @responsebody tag is reported in 406 error: Failed to load resource:the Server responded with a status of 406 (not Ac Ceptable) as well as the error description: The resource identified by this request are only capable of generating responses with characteristics not Acceptable according to the request "accept" headers () So, Baidu, Google for a long time, found that a lot of people encounter this problem, but are saying what to add Jackson what I am using the Fastjson , Jackson tried for half a day is still 406. Later in StackOverflow someone said it was spring 3.2 bug, and then returned to 3.1.*, no longer reported 406, although return 3.1 does not report the error, but still want to see how to handle AJAX return JSON data in the way of two versions in the end what difference, Debug. Debug to Org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConve Rters (T returnvalue,methodparameter returntype,servletserverhttprequest inputmessage,servletserverhttpresponse Outputmessage), throws an exception at the following code:
if (Compatiblemediatypes.isempty ()) {
throw new httpmediatypenotacceptableexception (allsupportedmediatypes);
}
It appears that compatiblemediatypes is the result of empty. Look at the debug information, after comparison found that 3.1 of Requestedmediatypes is [*/*], and 3.2 of Requestedmediatypes is [text/html],produciblemediatypes] [ Application/json] and then found the way to get Acceptablemediatypes 3.1 and 3.2 different 3.1
3.1 of
Private list<mediatype> getacceptablemediatypes (Httpinputmessage inputmessage) {
try {
L ist<mediatype> result = Inputmessage.getheaders (). getaccept ();
Return Result.isempty ()? Collections.singletonlist (Mediatype.all): result;
The catch (IllegalArgumentException ex) {
if (logger.isdebugenabled ()) {
Logger.debug ("C Ould Not parse Accept header: "+ ex.getmessage ());
}
return Collections.emptylist ();
}
}
3.2 of
Private list<mediatype> Getacceptablemediatypes (HttpServletRequest request) throws httpmediatypenotacceptableexception {
list<mediatype> mediatypes = this.contentNegotiationManager.resolv Emediatypes (Request) (new servletwebrequest);
Return Mediatypes.isempty ()? Collections.singletonlist (Mediatype.all): mediatypes;
}
It seems the problem is out here. I wonder why spring changes the way it is implemented ....
The workaround is as follows:
First, continue with spring 3.1.4.
Two, the second type
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:
Mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframewor K.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframewo Rk.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframewor K.org/schema/mvc Http://www.springframewOrk.org/schema/mvc/spring-mvc-3.0.xsd ">
Upgrade the spring-mvc-3.0.xsd to Spring-mvc-3.2.xsd
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:
Mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schem A/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema
/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spriNg-mvc-3.2.xsd ">
Then change the <mvc:annotation-driven> into the following format
<mvc:annotation-driven content-negotiation-manager= "Contentnegotiationmanager"/>
<bean id= " Contentnegotiationmanager "class=" Org.springframework.web.accept.ContentNegotiationManagerFactoryBean ">
<property name= "Favorpathextension" value= "false"/> <property "name="
Favorparameter "false"/ >
<property name= "Ignoreacceptheader" value= "false"/> <property name=
"MediaTypes" >
<value>
atom=application/atom+xml
html=text/html
json=application/json
*=*/*
</ value>
</property>
</bean>
Third, three
For details, see the address below to click on the Open link
Four, Fourth kind
Spring 3.2 When Requestedmediatypes is [text/html] 406 error, there is another reason may be due to the suffix, if the use of *.htm,*.html, and so on, by default will use [text/html] encoding, If you change it to *.json,*.shtml, it's OK.