Spring MVC使用@ResponseBody返回JSON數據406以及亂碼問題解決方案

其實前面一篇關於zTree返回JSON數據的文章已經有一種解決方案了,但是當我今天在新公司搭建新環境的時候,發現決然又不行了,所以我覺得那應該不是最優的解決方案。


說起來,我以前接觸到的一個項目,根本沒有配置spring的文件,就直接用@ResponseBody可以返回JSON數據,不知道其中的祕訣在什麼地方,搞不懂了。


今天主要提供另一個解決@ResponseBody返回JSON數據,頁面拋出406錯誤的解決方案。


第一步,引入包:


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>




第二步,修改Spring MVC的配置文件(高能預警:切記需要使用3.2及以上xsd),增加如下代碼:


修改於2015-8-26 16:24:58  解決了直接返回String時亂碼的問題




<!-- 解決@ResponseBody註解直接返回對象並轉換成JSON時出現406問題,同時解決了返回String類型亂碼的問題 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
<value>application/x-www-form-urlencoded; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


這次更新我將spring-mvc.xml的全部配置貼上來,方便大家看:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.2.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<context:component-scan base-package="com.busupport.apps" />

<!-- 解決@ResponseBody註解直接返回對象並轉換成JSON時出現406問題,同時解決了返回String類型亂碼的問題 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
<value>application/x-www-form-urlencoded; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


<!-- 視圖解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>


<!-- ======================== File Upload ============================= -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>utf-8</value>
</property>
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>


</beans>






再次測試,解決問題。


測試代碼如下:


1、JAVA代碼


/**
* 通過註解@ResponseBody返回JSON數據

* @return
*/
@RequestMapping("getdatabyresponsebody.json")
public @ResponseBody Map<String, Object> getAjaxDataByResponseBody() {
System.out.println("通過註解@ResponseBody返回JSON數據");
Map<String, Object> map = new HashMap<String, Object>();
map.put("success", true);
map.put("message", "Successfully returning the data.");
return map;
}
2、JS代碼




function getAjaxDataByResponseBody(){
$.ajax({
url:'ajax/getdatabyresponsebody.json',
type:'post',
dataType:'json',
success:function(data){
alert(data.success);
alert(data.message);
},
error:function(){
alert('System is wrong.');
}
});
}

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