生活随笔
收集整理的這篇文章主要介紹了
Spring 3.2.* MVC通过Ajax获取JSON数据报406错误
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring 3.2.x通過@ResponseBody標(biāo)簽返回JSON數(shù)據(jù)的方法都報(bào)406錯(cuò):?Failed to load resource: the server responded with a status of 406 (Not Acceptable)?以及報(bào)錯(cuò)描述:?The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () ?于是,百度、Google了半天,發(fā)現(xiàn)遇到此問題的人挺多的,但是都是說什么添加Jackson什么的,我是采用的fastjson,換成Jackson嘗試了半天均還是406。?后來在stackoverflow有人說是Spring 3.2的BUG,于是退回到3.1.*,不再報(bào)406了,?雖然換回 3.1不報(bào)錯(cuò)了,但還是想看看在處理ajax返回json數(shù)據(jù)的方式上兩個(gè)版本到底有何區(qū)別,debug之。 debug到 org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(T returnValue,MethodParameter returnType,ServletServerHttpRequest inputMessage,ServletServerHttpResponse outputMessage),在以下代碼處拋出了異常:
?
[java] view plaincopy
if?(compatibleMediaTypes.isEmpty())?{?? ????????throw?new?HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);?? ????}?? 看 來是compatibleMediaTypes為空導(dǎo)致。看debug信息,經(jīng)過比較發(fā)現(xiàn)3.1的requestedMediaTypes為[*/*], 而3.2的requestedMediaTypes卻為[text/html],producibleMediaTypes都是[application /json],繼而發(fā)現(xiàn)獲取acceptableMediaTypes的方式3.1與3.2不同 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;?? ????}?? 看來問題就是出在這里了。不知Spring為何改變?cè)搶?shí)現(xiàn)方式??!!?
?
解決方法如下:
一、第一種?繼續(xù)用Spring 3.1.4。
二、第二種
?
[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">?? 把spring-mvc-3.0.xsd 升級(jí)到?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">?? ?
然后把<mvc:annotation-driven>修改成如下格式
?
[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>?? 三、第三種
?
詳情見下面的地址點(diǎn)擊打開鏈接
四、第四種
spring 3.2時(shí)requestedMediaTypes卻為[text/html]的情況報(bào)406錯(cuò)誤,還有一個(gè)原因可能是由于采用的后綴有關(guān),如果使 用*.htm,*.html等,默認(rèn)就會(huì)采用[text/html]編碼,若改成*.json,*.shtml等就OK
?
文章來源:http://blog.csdn.net/gbtyy/article/details/17165605
轉(zhuǎn)載于:https://www.cnblogs.com/hhang/p/4286576.html
總結(jié)
以上是生活随笔 為你收集整理的Spring 3.2.* MVC通过Ajax获取JSON数据报406错误 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。