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.');
}
});
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值