Spring 3.2.* MVC通過Ajax獲取JSON數據報406錯誤

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),在以下代碼處拋出了異常:

[java] view plaincopy
  1. if (compatibleMediaTypes.isEmpty()) {  
  2.         throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);  
  3.     }  

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

3.1的

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. private List<MediaType> getAcceptableMediaTypes(HttpInputMessage inputMessage) {  
  2.         try {  
  3.             List<MediaType> result = inputMessage.getHeaders().getAccept();  
  4.             return result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result;  
  5.         } catch (IllegalArgumentException ex) {  
  6.             if (logger.isDebugEnabled()) {  
  7.                 logger.debug("Could not parse Accept header: " + ex.getMessage());  
  8.             }  
  9.             return Collections.emptyList();  
  10.         }  
  11.     }  
3.2的

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {  
  2.         List<MediaType> mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));  
  3.         return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes;  
  4.     }  
看來問題就是出在這裏了。不知Spring爲何改變該實現方式??!! 

解決方法如下:

一、第一種 繼續用Spring 3.1.4。

二、第二種

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.        
  7.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.              http://www.springframework.org/schema/context  
  10.              http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.              http://www.springframework.org/schema/aop  
  12.              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.              http://www.springframework.org/schema/tx   
  14.              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  15.              http://www.springframework.org/schema/mvc    
  16.              http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

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

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.          http://www.springframework.org/schema/context  
  10.          http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.          http://www.springframework.org/schema/aop  
  12.          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.          http://www.springframework.org/schema/tx   
  14.          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  15.         http://www.springframework.org/schema/mvc   
  16.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  

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

[html] view plaincopy
  1. <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />   
  2. <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">   
  3.     <property name="favorPathExtension" value="false" />  
  4.     <property name="favorParameter" value="false" />   
  5.     <property name="ignoreAcceptHeader" value="false" />   
  6.     <property name="mediaTypes" >   
  7.         <value>  
  8.             atom=application/atom+xml  
  9.             html=text/html  
  10.             json=application/json  
  11.             *=*/*  
  12.         </value>   
  13.     </property>  
  14. </bean>  
三、第三種

詳情見下面的地址點擊打開鏈接

四、第四種

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章