springMVC瀏覽器接受json報406錯誤的解決方案,springmvcjson

來源:互聯網
上載者:User

springMVC瀏覽器接受json報406錯誤的解決方案,springmvcjson

這個錯誤還真難搞,頭一次遇見還真沒有什麼好辦法解決,上網找了很多資料,一一試過也不能很好的解決,最後經過朋友,同事一起努力,終於徵服了.留下筆記,供大家參考:

1.我後台測試代碼如下:

@RequestMapping("/json")public @ResponseBodyString json() {return "this is json";}@RequestMapping("/json2")public @ResponseBodyList<User> json2() {List<User> users = new ArrayList<User>();User u = new User();u.setAccount("aa");users.add(u);return users;}

在瀏覽器訪問這兩個方法時,只有json正確,而json2就報406,說明spring沒有處理好對象序列化成json的問題,最後我通過log4j列印的日誌,也清晰的看到他返回的格式並非json格式.

既然格式錯誤了,瀏覽器當然接受不了資料,原因就在此了


2.知道了原因,於是就上網搜了.網上有人說mvc的xml沒有加這個配置

<!-- 開啟註解 --><mvc:annotation-driven />

但是我項目早就有了,固然不是問題答案


另外一種是說要配置json模板,覺得這個靠譜,於是乎就學著配置.....終於掉進坑裡了

        <!-- 避免IE執行AJAX時,返回JSON出現下載檔案 --><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉換器 --></list></property></bean>

於是配置了一下,啟動項目就報錯了,缺包緣故,於是上網找jar包,這又遇到版本問題這個還真不容易,晚上資料卻沒說版本不同要找不到jar

這裡我特意說明下

spring 4.0或一下的,一般都找jackson 1.x的版本

spring 4.1或以上的.就要找jackson2.x以上了

我的項目引用的就是spring 4.1.2的版本,一開始不清楚狀況,盲目按網上的人配置,引用jackson版本比較低,到頭來問題沒解決,還引來一堆別的問題,頭痛的要死,死去活來.....

哎......不堪回頭...好吧,迴歸主題



以下是spring 4.1.2版本的正確配置

<----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->

4.1.2的

<!-- 避免IE執行AJAX時,返回JSON出現下載檔案 --><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉換器 --></list></property></bean>

引入三個jackson2.4版本

如下:

jackson-core-2.4.1.jar

jackson-annotations-2.4.1.jar

jackson-databind-2.4.1.jar

問題就解決了,不在報406了,json資料出來了


三個jar我上傳,供大家下載~

地址:http://download.csdn.net/detail/qq183293/8378947

謝謝大家的支援~


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.