在使用SpringMVC框架直接返回json数据给client时,不同的版本号有差异。

以下介绍两种类型的版本号怎样配置。

注意:这两种方法均已验证通过。

1、Spring3.1.x版本号

1.1 dispatcher-servlet.xml配置文件例如以下:

<?

xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">





<mvc:annotation-driven />

<!-- only usable before 3.2 -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      

        <property name="messageConverters">      

            <list >      

                <ref bean="mappingJacksonHttpMessageConverter" />      

            </list>      

        </property>      

    </bean>  

    

    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    

   <property name="supportedMediaTypes">    

       <list>    

           <value>application/json;charset=UTF-8</value>    

      </list>    

   </property>  

</bean>  



</beans>

注意高亮部分。

1.2 pom.xml文件增加例如以下依赖:

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-core-asl</artifactId>

<version>1.9.13</version>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.13</version>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-core-lgpl</artifactId>

<version>1.9.13</version>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-lgpl</artifactId>

<version>1.9.13</version>

</dependency>

2、Spring4.x版本号

2.1 dispatcher-servlet.xml文件相关配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">



<!-- <mvc:annotation-driven /> -->



<mvc:annotation-driven>

        <mvc:message-converters register-defaults="true">

            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">

                <property name="supportedMediaTypes">

                    <list>

                        <value>application/json;charset=UTF-8</value>

                    </list>

                </property>

            </bean>

        </mvc:message-converters>

    </mvc:annotation-driven>



          

</beans>

注意:须要使用spring-mvc-3.2.xsd文件;

2.2 pom.xml文件增加例如以下依赖:

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.6</version>

</dependency>

Spring MVC返回json格式的更多相关文章

  1. Spring mvc 返回json格式 - 龙企阁 - 博客频道 - CSDN.NET

    第一次使用spring mvc ,在此也算是记录一下以防忘记,希望有经验的朋友指出不足的地方 一.使用maven管理jar. <dependency> <groupId>org ...

  2. spring mvc返回json格式和json字符串

    首先有必要说一下,json和json字符串是不一样的,后者是一个字符串.而json是一个对象 当然如果调用位置是后台程序这几乎没有区别,因为在后台,无论什么格式数据,都是从响应流中读取字符串. 但是在 ...

  3. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  4. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  5. Spring MVC 返回json数据 报406错误 问题解决方案

    将jackson jar包改为jackson-databind-2.5.0.jar  jackson-core-2.5.0.jar  jackson-annotations-2.5.0.jar(这个版 ...

  6. Spring MVC返回Map格式JSON数据

    问题描述: ajax中走error : function(e) {} 问题背景: 在测试controller层时,试过了ResponseEntity<ResponseModel>这种类型返 ...

  7. 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位

    转载http://ks.netease.com/blog?id=4024 作者:李景     场景:          前端Post请求同一个url地址,在chrome浏览器上有正常返回json,而在 ...

  8. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  9. spring mvc 返回json的配置

    转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

随机推荐

  1. 【linux】查看内存和CPU使用情况

    1.内存命令:free 解释:以上数据单位KB. 所以,上面的mem物理内存共1G 下面是对这些数值的解释: total:总计物理内存的大小. used:已使用多大. free:可用有多少. Shar ...

  2. python --中文相关问题

    往文件中写入中文,再从文件中读出,范例1: #coding:utf- with open('data.log','w') as f: a=u'中文' b = a.encode('gbk') f.wri ...

  3. xaf 修改主页logo

    https://documentation.devexpress.com/#Xaf/CustomDocument3156

  4. No persister for nhibernate 解决下面的问题

    在你的实体类对应的配置文件点右键选择属性,修改类型为:一直复制和嵌入的资源.就可以了.

  5. Atitit.数据库事务隔离级别 attilax 总结

    Atitit.数据库事务隔离级别 1. 事务隔离级别的作用 1 2. 在的隔离级别 2 3. 常见数据库的默认管理级别 3 1. 事务隔离级别的作用 较低的隔离级别可以增强许多用户同时访问数据的能力, ...

  6. 云中的机器学习:FPGA 上的深度神经网络

    人工智能正在经历一场变革,这要得益于机器学习的快速进步.在机器学习领域,人们正对一类名为“深度学习”算法产生浓厚的兴趣,因为这类算法具有出色的大数据集性能.在深度学习中,机器可以在监督或不受监督的方式 ...

  7. bazel-编译多目标

    demo2 使用bazel编译多目标示例,一个bianry,一个library. demo2目录树 ── demo2 ​ ├── app ​ │ ├── BUILD ​ │ ├── func.cpp ...

  8. line: 1: Syntax error: word unexpected (expecting ")")

    开发板上运行可执行程序报出错误: line1: 1: Syntax error: word unexpected (expecting ")") 解决思路: 1.编译器的问题 用a ...

  9. 在linux下监控文件是否被删除或创建的命令

    You can use auditd and add a rule for that file to be watched: auditctl -w /path/to/that/file -p wa ...

  10. linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结(转载)

    Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...