在使用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. 使用Eclipse构建Maven项目 (step-by-step) (转收藏)

    Maven这个个项目管理和构建自动化工具,越来越多的开发人员使用它来管理项目中的jar包.本文仅对Eclipse中如何安装.配置和使用Maven进行了介绍.完全step by step. 如果觉得本文 ...

  2. OFBiz:初始RequestHandler

    RequestHandler,可以称之为请求处理器,在ControlServlet.init()中初始化: public class ControlServlet extends HttpServle ...

  3. Linux程序调试GDB——数据查看

    查看栈信息 当程序被停住了,首先要确认的就是程序是在哪儿被断住的.这个一般是通过查看调用栈信息来看的.在gdb中,查看调用栈的命令是backtrace,可以简写为bt. (gdb) bt    #0 ...

  4. Redis总结(五)缓存雪崩和缓存穿透等问题(转载)

    前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhong/category/771056.html .今 ...

  5. AFLW如何获取你想要的21点人脸关键点数据

    目前人脸检测和人脸的关键点的数据库根据关键点个数:5,20,21,29,68等.https://blog.csdn.net/XZZPPP/article/details/74939823该网页详细列出 ...

  6. sql 百分比

    select [city], bfb=cast(cast(count(*)*100./(select count(*) from [UserBasicInfo]) as decimal(10,0)) ...

  7. JavaScript身份证正则表达式,querySelector通过name获取元素,php正则表达式

    var preg_sfz1 = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3 ...

  8. What is purpose of @ConditionalOnProperty annotation?

    http://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation **** ...

  9. 基于jdom 的 xmluti

    package cn.com.do1.wechat.common; import org.jdom.Attribute; import org.jdom.Document; import org.jd ...

  10. zepto中的tap穿透

    有一个项目,浮层上是有点击的按钮,但是用tap就会穿透,触发浮层下的页面的点击事件.后来问同事和经过自己尝试,发现用click就可以解决这个问题.