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

3.1

[java] view plaincopy
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 ();
        }
    }
3.2

 

[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? ? !! !!

 

The solution is as follows:

First, continue to use Spring 3.1.4.

Second, the second

 

[html] view plaincopy
<? xml version = "1.0" encoding = "UTF-8"?>
    <beans xmlns = "http://www.springframework.org/schema/beans"
    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/schema/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.0.xsd ">
Upgrade spring-mvc-3.0.xsd to spring-mvc-3.2.xsd

 

[html] view plaincopy
<? xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
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/schema/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 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.

 

Article source: http://blog.csdn.net/gbtyy/article/details/17165605

Spring 3.2. * MVC gets JSON datagram 406 error via Ajax
Related Article

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.