<?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.2.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.2.xsd">
  <context:component-scan base-package="com.jadyer"/>

  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
        <property name="features">
          <array>
            <value>WriteMapNullValue</value>
            <value>WriteNullStringAsEmpty</value>
          </array>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>

  <!-- fastjson-1.1.41与SpringMVC整合 -->
  <!--
  1)若按照jackson和SpringMVC的整合方式,应按照下面的写法,但测试发现这样会报告"HTTP Status 406"
    The resource identified by this request is only capable of generating responses
    with characteristics not acceptable according to the request "accept" headers.
  2)测试通过的整合方式为上面那样在mvc:annotation-driven里面进行注册
  3)supportedMediaTypes增加[text/html;charset=UTF-8]值,是为了兼容IE6
    否则[application/json]值在IE6中会导致弹出对话框询问是否保存文件,而firefox等高级浏览器会正常打印json字符串
  4)若像下面这样给supportedMediaTypes属性赋两个值[text/html;charset=UTF-8]和[application/json],则[application/json]是无效的
    因为此时应答给浏览器(或者说请求方)的Content-Type头信息都是[text/html;charset=UTF-8],所以给它一个值就行了
    如果给supportedMediaTypes的值为[application/json],则应答给浏览器的Content-Type头信息就是[application/json;charset=UTF-8]
  5)关于features属性,不是serializerFeature,而是features,详见FastJsonHttpMessageConverter.java
    它是用来控制json序列化输出时的一些额外属性,比如说该字段是否输出、输出时key使用单引号还是双引号、key不使用任何引号等等
    QuoteFieldNames----------输出key时是否使用双引号,默认为true
    WriteMapNullValue--------是否输出值为null的字段,默认为false
    WriteNullNumberAsZero----数值字段如果为null,输出为0,而非null
    WriteNullListAsEmpty-----List字段如果为null,输出为[],而非null
    WriteNullStringAsEmpty---字符类型字段如果为null,输出为"",而非null
    WriteNullBooleanAsFalse--Boolean字段如果为null,输出为false,而非null
  6)通常在网上搜到的fastjson和springMVC整合的例子中都像下面注释的代码那样给了两个属性WriteMapNullValue和QuoteFieldNames
    这就表示为json解析器设置QuoteFieldNames和WriteMapNullValue的值为true,即输出时key使用双引号,同时也输出值为null的字段
  7)输出时某字段为String类型,且值为null,此时若需要其输出,且输出值为空字符串,则需同时赋值WriteMapNullValue和WriteNullStringAsEmpty
    经测试,若只赋值WriteNullStringAsEmpty,则不会输出该字段..加上WriteMapNullValue属性后,便输出了,且输出值不是null,而是预期的空字符串
   -->
  <!--
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <list>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
          <property name="supportedMediaTypes">
            <list>
              <value>text/html;charset=UTF-8</value>
              <value>application/json</value>
            </list>
          </property>
          <property name="serializerFeature">
            <array>
              <value>QuoteFieldNames</value>
              <value>WriteMapNullValue</value>
            </array>
          </property>
        </bean>
      </list>
    </property>
  </bean>
   -->
  <!--
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <list>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
          <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
        </bean>
      </list>
    </property>
  </bean>
   -->
</beans>

fastjson对于循环引用的处理比较反人类.fastjson可以用引用来表示相同的对象.

{

x:{"name":"weidiao","age":24}

y:{"name":"weidiao","age":24}

}

fastjson会将其写为

{

x:{"name":"weidiao","age":24}

y:$1

}

如果服务器端和客户端都是使用fastjson,则一切正常.否则客户端是无法理解fastjson产生的json串的.这样使用引用的好处在于

1.节省生成的字符串的长度

2.解析json串时,可以完完全全的还原状态,使两个引用指向同一个对象.

fastjson+mybatis一级缓存一起使用,在使用association查询时,就会产生十分神奇的现象(乱七八糟).

springmvc整合fastjson的更多相关文章

  1. SpringMVC整合fastjson、easyui 乱码问题

    一.框架版本 SpringMVC:3.1.1.RELEASE fastjson:1.2.7 easyui :1.4.5 二.乱码现象    Action中使用@ResponseBody返回Json数据 ...

  2. SpringMVC整合FastJson:用"最快的json转换工具"替换SpringMVC的默认json转换

    2017年11月23日 09:18:03 阅读数:306 一.环境说明 Windows 10 1709 Spring 4.3.12.RELEASE FastJson 1.2.40 IDEA 2017. ...

  3. SpringMVC整合fastjson-1.1.41

    以前用fastjson也只是硬编码,就好像这篇博文写的http://blog.csdn.net/jadyer/article/details/24395015 昨天心血来潮突然想和SpringMVC整 ...

  4. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  5. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  6. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  7. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  8. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  9. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

随机推荐

  1. C语言中不同函数之间怎么传值?

    #include <stdio.h> int change(); int change(int j) { j=; return(j); } void main() { int b = ch ...

  2. WCF自定义Header

    MiscWebSrvcInfClient client = new MiscWebSrvcInfClient("MiscWSBeanPort", ConfigurationMana ...

  3. AC日记——字符环 openjudge 1.7 30

    30:字符环 总时间限制:  1000ms 内存限制:  65536kB 描述 有两个由字符构成的环.请写一个程序,计算这两个字符环上最长连续公共字符串的长度.例如,字符串“ABCEFAGADEGKA ...

  4. [No000046]为什么跳槽加薪会比内部调薪要高?

    有网友在知乎提问: 最近在思考一个问题,为什么跳槽往往意味着加薪? 如果一个人确有价值,为什么在原来的公司没有在薪水上体现出来?如果没有价值,为什么跳槽以后就会加薪?还是可以单纯的解释为,应聘者和招聘 ...

  5. Windows 7 USB DVD Download Tool

    1.下载.安装.运行Windows 7 USB DVD Download Tool: 百度官方下载 http://rj.baidu.com/soft/detail/20458.html 2.点击&qu ...

  6. UIButton(改变Title和image位置)

    UIButton *btn = [[UIButton alloc] init]; [btn setFrame:frame]; [btn setTitleColor:titleColor forStat ...

  7. http协议(十)实体首部字段

    1.定义 包含在请求和响应中的实体部分所使用的首部,用于补充内容的更新时间等与实体相关的信息 2.Allow 通知客户端能够支持的Request-URI指定资源的所有http方法 如果服务器接收到不支 ...

  8. c++ 基于wincrypt的DES CBC模式加解密

    des.h #pragma once #include <windows.h> #include <atlstr.h> #include <wincrypt.h> ...

  9. Memcached和Memcache安装(64位win7)

    一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...

  10. FMDB处理动态插入语句

    昨天做一个需求,参数的数量不确定,所以无法使用这个API: - (BOOL)executeUpdate:(NSString*)sql, ... 但是用 - (BOOL)executeUpdate:(N ...