Spring 3.2.* MVC gets JSON datagram 406 error via Ajax
Source: Internet
Author: User
Spring 3.2.x methods that return JSON data through the @ResponseBody tag all report 406 errors: Failed to load resource: the server responded with a status of 406 (Not Acceptable) and an error description: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () So, Baidu and Google for a long time, and found that many people encountered this problem, but they all say that the addition of Jackson or something, I use fastjson, Switched to Jackson and tried 406 for half a day. Later on stackoverflow, some people said that it was a bug in Spring 3.2, so it returned to 3.1. *, And no longer reported 406. Although it is not reported as an error in 3.1, I still want to see the two versions in the way of processing ajax to return json data. What's the difference, debug it. Debug to org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters (T returnValue, MethodParameter returnType, ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage), and throw an exception at the following code:
[java] view plaincopy
if (compatibleMediaTypes.isEmpty ()) {
throw new HttpMediaTypeNotAcceptableException (allSupportedMediaTypes);
}
It seems that compatibleMediaTypes is empty. Looking at the debug information, after comparison, it was found that the requestedMediaTypes of 3.1 was [* / *], while the requestedMediaTypes of 3.2 were [text / html], and the producedMediaTypes were all [application / json], and then it was found that the method 3.1 for obtaining acceptableMediaTypes is different from 3.2 for 3.1
[java] view plaincopy
private List <MediaType> getAcceptableMediaTypes (HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
List <MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes (new ServletWebRequest (request));
return mediaTypes.isEmpty ()? Collections.singletonList (MediaType.ALL): mediaTypes;
}
It seems that the problem lies here. I wonder why Spring changed the implementation? ? !! !!
Then modify <mvc: annotation-driven> to the following format
[html] view plaincopy
<mvc: annotation-driven content-negotiation-manager = "contentNegotiationManager" />
<bean id = "contentNegotiationManager" class = "org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name = "favorPathExtension" value = "false" />
<property name = "favorParameter" value = "false" />
<property name = "ignoreAcceptHeader" value = "false" />
<property name = "mediaTypes">
<value>
atom = application / atom + xml
html = text / html
json = application / json
* = * / *
</ value>
</ property>
</ bean>
Third, the third
See the address below for details
Fourth, the fourth
In Spring 3.2, when the requestedMediaTypes is [text / html], it reports a 406 error. Another reason may be that the suffix used is related. If you use * .htm, * .html, etc., it will use [text / html] encoding by default. If you change it to * .json, * .shtml, etc.
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.