Jackson spring的兼容性比较好,推荐使用,不要轻易使用fastjson来做controller转换
fastjson SerializerFeature详解 http://blog.csdn.net/u010246789/article/details/52539576
springmvc fastjson与Jackson的MessageConverter配置 http://ibear.me/2016/02/15/170 这里说明版本问题
原文: http://blog.51cto.com/boytnt/1771390
在servlet.xml配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <!--设置fastjson特性--> <property name="features"> <array> <!--设置null值也要输出,fastjson默认是关闭的--> <value>WriteMapNullValue</value> <!--设置使用文本方式输出日期,fastjson默认是long--> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> |
这篇文章配置: https://github.com/alibaba/fastjson/issues/946
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig"> <!-- Default charset --> <property name="charset" value="UTF-8" /> <!-- SerializerFeature --> <property name="serializerFeatures"> <list> <value>QuoteFieldNames</value> <value>WriteMapNullValue</value> </list> </property> </bean> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!--将StringHttpMessageConverter的默认编码设为UTF-8--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!-- 配置Fastjson支持 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> <property name="fastJsonConfig" ref="fastJsonConfig"/> </bean> </mvc:message-converters> </mvc:annotation-driven> |
最后测试经过的配置:
1 2 3 |
<bean name="utf8Charset" class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value="UTF-8"></constructor-arg> </bean> |
A1: 默认1
1 2 3 4 5 6 7 8 9 10 11 |
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset"/> </bean> |
A2: 默认2
1 2 3 4 5 6 7 8 |
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset"/> </bean> </mvc:message-converters> </mvc:annotation-driven> |
B: Jackson spring的兼容性比较好,推荐使用,不要轻易使用fastjson来做controller转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/xml;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> |
C: FastJson: spring版本: 4.3.13.RELEASE ; fastjson版本:1.2.45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset" index="0"></constructor-arg> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> </list> </property> </bean> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> <property name="fastJsonConfig"> <bean class="com.alibaba.fastjson.support.config.FastJsonConfig"> <property name="features"> <list> <value>DisableCircularReferenceDetect</value> </list> </property> <!-- 不要轻易使用这个格式化,避免代码的兼容 --> <!--<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>--> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> |
输出:
1 2 3 4 5 6 7 8 9 10 11 |
A1===> {"sessionData":{"sessionId":"24a2ecc8-8637-445e-814d-d7a2a75c93b9","instanceId":1,"instanceCode":"FX","logId":170233,"userId":2,"userCode":"FXRoot","userName":"枫香超级管理员","email":null,"mobile":null,"orgIds":null,"orgName":"枫香","staffId":2,"staffName":null,"orgId":1,"roleId":1,"roleCode":"FXRoot","roleName":"默认管理员","remoteHost":"0:0:0:0:0:0:0:1","remoteAddr":"0:0:0:0:0:0:0:1","loginTime":1517362295364,"lastAccessedTime":null,"admin":true,"supper":false,"clientOs":null,"terminal":null,"userAgent":null},"formList":[],"date2":1517328000000,"date1":1517362464080} A2===> {"sessionData":{"sessionId":"688e34c8-d699-4157-8aea-50eb2bcdcb70","instanceId":1,"instanceCode":"FX","logId":170235,"userId":2,"userCode":"FXRoot","userName":"枫香超级管理员","email":null,"mobile":null,"orgIds":null,"orgName":"枫香","staffId":2,"staffName":null,"orgId":1,"roleId":1,"roleCode":"FXRoot","roleName":"默认管理员","remoteHost":"0:0:0:0:0:0:0:1","remoteAddr":"0:0:0:0:0:0:0:1","loginTime":1517364567298,"lastAccessedTime":null,"admin":true,"supper":false,"clientOs":null,"terminal":null,"userAgent":null},"formList":[],"date2":1517328000000,"date1":1517364619194} B ===> {"sessionData":{"sessionId":"4c36a5bd-6b45-48b8-bd45-32c8f944d1f2","instanceId":1,"instanceCode":"FX","logId":170237,"userId":2,"userCode":"FXRoot","userName":"枫香超级管理员","email":null,"mobile":null,"orgIds":null,"orgName":"枫香","staffId":2,"staffName":null,"orgId":1,"roleId":1,"roleCode":"FXRoot","roleName":"默认管理员","remoteHost":"0:0:0:0:0:0:0:1","remoteAddr":"0:0:0:0:0:0:0:1","loginTime":1517364957603,"lastAccessedTime":null,"admin":true,"supper":false,"clientOs":null,"terminal":null,"userAgent":null},"formList":[],"date2":1517328000000,"date1":1517364994073} 不格式化日期 C1===> {"sessionData":{"admin":true,"instanceCode":"FX","instanceId":1,"logId":170257,"loginTime":1517373598941,"orgId":1,"orgName":"枫香","remoteAddr":"0:0:0:0:0:0:0:1","remoteHost":"0:0:0:0:0:0:0:1","roleCode":"FXRoot","roleId":1,"roleName":"默认管理员","sessionId":"3fab7eff-d2f0-48c0-a3a9-082e37df8811","staffId":2,"supper":false,"userCode":"FXRoot","userId":2,"userName":"枫香超级管理员"},"formList":[],"date2":1517328000000,"date1":1517373621168} 格式化日期 C1===> {"sessionData":{"admin":true,"instanceCode":"FX","instanceId":1,"logId":170255,"loginTime":"2018-01-31 11:53:58","orgId":1,"orgName":"枫香","remoteAddr":"0:0:0:0:0:0:0:1","remoteHost":"0:0:0:0:0:0:0:1","roleCode":"FXRoot","roleId":1,"roleName":"默认管理员","sessionId":"19bcf755-bdba-470e-a73d-2b93c142f88c","staffId":2,"supper":false,"userCode":"FXRoot","userId":2,"userName":"枫香超级管理员"},"formList":[],"date2":"2018-01-31 00:00:00","date1":"2018-01-31 11:54:10"} |