Spring MVC Rest服務 返回json報406錯誤的解決辦法


@ResponseBody & @RequestBody

@RequestBody 將 HTTP 請求正文插入方法中,使用適合的HttpMessageConverter將請求體寫入某個對象。

@ResponseBody 將內容或對象作為 HTTP 響應正文返回,使用@ResponseBody將會跳過視圖處理部分,而是調用適合HttpMessageConverter,將返回值寫入輸出流。

@ResponseBody可以標注任何對象,由Srping完成對象——協議的轉換

我們看到,短短幾行配置。使用@ResponseBody注解之后,Controller返回的對象 自動被轉換成對應的json數據,在這里不得不感嘆SpringMVC的強大。

昨天在做@ResponseBody返回JSON格式的時候,老是報http 406錯誤,仔細查看了配置文件,原來是出現了兩個配置。導致后面那個失效所致,下面給出簡單排查和幾種解決方案

出錯的大致意思是 :

HTTP Status 406 (不接受) 

->無法使用請求的內容特性響應請求的網頁。

其中網上很多資料都是說supportedMediaTypes需要添加application/json;charset=UTF-8,但依然出現406 (Not Acceptable)

一:確保applicationContext-configuration.xml配置了<mvc:annotation-driven>

 1     <mvc:annotation-driven>
 2         <mvc:message-converters>
 3             <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 4                 <property name="supportedMediaTypes">
 5                     <list>
 6                         <value>text/plain;charset=utf-8</value>
 7                         <value>text/html;charset=UTF-8</value>
 8                         <value>text/json;charset=UTF-8</value>
 9                         <value>application/json;charset=utf-8</value>
10                     </list>
11                 </property>
12                 <property name="objectMapper">
13                     <bean class="com.fasterxml.jackson.databind.ObjectMapper">
14                         <property name="dateFormat">
15                             <bean class="java.text.SimpleDateFormat">
16                                 <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
17                             </bean>
18                         </property>
19                     </bean>
20                 </property>
21             </bean>
22         </mvc:message-converters>
23     </mvc:annotation-driven>

 

二:原來springMvc使用@ResponseBody,如果返回的是json結果,需要添加jackson的jar包的依賴

 1 <dependency>
 2     <groupId>org.codehaus.jackson</groupId>
 3     <artifactId>jackson-core-asl</artifactId>
 4     <version>1.9.13</version>
 5 </dependency>
 6     <dependency>
 7         <groupId>org.codehaus.jackson</groupId>
 8         <artifactId>jackson-mapper-asl</artifactId>
 9         <version>1.9.13</version>
10     </dependency>
11 </dependencies>
12 <dependency>
13     <groupId>com.fasterxml.jackson.core</groupId>
14     <artifactId>jackson-databind</artifactId>
15     <version>2.8.0</version>
16 </dependency>

三、測試supportedMediaTypes,就算不配置application/json;charset=UTF-8,也可以正常返回結果。

通過以下測試,確保正確返回

1 <property name="supportedMediaTypes">
2     <list>
3         <value>text/plain;charset=utf-8</value>
4         <value>text/html;charset=UTF-8</value>
5         <value>text/json;charset=UTF-8</value>
6         <value>application/json;charset=utf-8</value>
7     </list>
8 </property>

注意:在使用@ResponseBody 返回json的時候,方法參數中一定不能他添加   PrintWriter printWriter,這就畫蛇添足了,而且程序會報錯

 java.lang.IllegalStateException: getWriter() has already been called for this response

 

 

 參考:

https://my.oschina.net/lichhao/blog/172562

https://my.oschina.net/HeliosFly/blog/205343

http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-convert.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM