Spring 3.2.* MVC通過Ajax擷取JSON資料報406錯誤__Ajax

來源:互聯網
上載者:User

Spring 3.2.x通過@ResponseBody標籤返回JSON資料的方法都報406錯: Failed to load resource: the server responded with a status of 406 (Not Acceptable) 以及報錯描述: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () 於是,百度、Google了半天,發現遇到此問題的人挺多的,但是都是說什麼添加Jackson什麼的,我是採用的fastjson,換成Jackson嘗試了半天均還是406。 後來在stackoverflow有人說是Spring 3.2的BUG,於是退回到3.1.*,不再報406了, 雖然換回3.1不報錯了,但還是想看看在處理ajax返回json資料的方式上兩個版本到底有何區別,debug之。 debug到org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(T returnValue,MethodParameter returnType,ServletServerHttpRequest inputMessage,ServletServerHttpResponse outputMessage),在以下代碼處拋出了異常:

if (compatibleMediaTypes.isEmpty()) {    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);}

看來是compatibleMediaTypes為空白導致。看debug資訊,經過比較發現3.1的requestedMediaTypes為[*/*],而3.2的requestedMediaTypes卻為[text/html],producibleMediaTypes都是[application/json],繼而發現擷取acceptableMediaTypes的方式3.1與3.2不同 3.1的

3.1的

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的

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;    }
看來問題就是出在這裡了。不知Spring為何改變該實現方式。。。。 

解決方案如下:

一、第一種 繼續用Spring 3.1.4。

二、第二種

<?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">

把spring-mvc-3.0.xsd 升級到 spring-mvc-3.2.xsd

<?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">

然後把<mvc:annotation-driven>修改成如下格式

<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/htmljson=application/json*=*/*</value> </property></bean>
三、第三種

詳情見下面的地址點擊開啟連結

四、第四種

spring 3.2時requestedMediaTypes卻為[text/html]的情況報406錯誤,還有一個原因可能是由於採用的尾碼有關,如果使用*.htm,*.html等,預設就會採用[text/html]編碼,若改成*.json,*.shtml等就OK

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.