SpringMVC整合fastjson
来源:互联网 发布:boxers知乎 编辑:程序博客网 时间:2024/06/06 10:04
springmvc在@ResponseBody时,默认使用Jackson做json对象的序列化操作。由于Jackson在处理对象的循环嵌套关系时不便,所以想用fastjson来替换。
方法,修改springmvc.xml
<!-- 基于Annotation的映射方式 --><mvc:annotation-driven > <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> --> <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的mvn即可。
补充:
1)supportedMediaTypes增加[text/html;charset=UTF-8]值,是为了兼容IE6
否则[application/json]值在IE6中会导致弹出对话框询问是否保存文件,而firefox等高级浏览器会正常打印json字符串
2)若像下面这样给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]
3)关于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
4)通常在网上搜到的fastjson和springMVC整合的例子中都像下面注释的代码那样给了两个属性WriteMapNullValue和QuoteFieldNames
这就表示为json解析器设置QuoteFieldNames和WriteMapNullValue的值为true,即输出时key使用双引号,同时也输出值为null的字段
5)输出时某字段为String类型,且值为null,此时若需要其输出,且输出值为空字符串,则需同时赋值WriteMapNullValue和WriteNullStringAsEmpty
经测试,若只赋值WriteNullStringAsEmpty,则不会输出该字段..加上WriteMapNullValue属性后,便输出了,且输出值不是null,而是预期的空字符串
- SpringMVC 整合fastjson
- SpringMVC整合fastjson
- SpringMVC整合fastjson
- springMVC 与 fastjson整合
- SpringMVC整合fastjson
- SpringMVC整合fastjson
- SpringMVC整合fastjson
- springmvc整合fastjson
- SpringMVC 4.1 整合fastjson
- SpringMVC 4.1 整合fastjson
- SpringMVC整合fastjson-1.1.41
- SpringMVC+FastJson整合完整示例
- springMVC整合FastJson实现RestFul风格API
- springMVC整合FastJson实现RestFul风格API
- FASTJSON 与 springmvc 整合,FastJsonHttpMessageConverter的配置
- SpringMVC+FastJson+hibernate-validator整合完整示例
- SpringMVC整合FastJson及简单使用
- Springmvc + FastJson
- iOS OpenGL ES 专业术语
- 生命的思考.纪念雷博
- HDOJ-----5137最短路
- 剑指offer - 从尾到头打印链表
- javascript之数组、ById、ByTagName、循环、cssText和this
- SpringMVC整合fastjson
- Hdu 4497 GCD and LCM(数论)
- C++对象模型
- 深入讲解Android中Activity launchMode
- linux文件系统
- F4 help functionality for a dynpro screen field using the PROCESS ON VALUE-REQUEST statement
- django ifequal标签的使用方式
- mybities insert 的时候得到主键的值
- 不敢死队问题