FastJson 序列化与反序列化一些说明

最近所属的组需要对接一些征信结构,就涉及到很多中的数据格式,而springmvc中使用的是jackson作为@ResponseBody的依赖jar 

但是个人认为fastkson的性能要高于jackson所以,希望返回的json格式是采用fastjson而非jackson

特此记录:

  首先请参照:SerializerFeature属性

名称含义备注
QuoteFieldNames 输出key时是否使用双引号,默认为true  
UseSingleQuotes 使用单引号而不是双引号,默认为false  
WriteMapNullValue 是否输出值为null的字段,默认为false  
WriteEnumUsingToString Enum输出name()或者original,默认为false  
UseISO8601DateFormat Date使用ISO8601格式输出,默认为false  
WriteNullListAsEmpty List字段如果为null,输出为[],而非null  
WriteNullStringAsEmpty 字符类型字段如果为null,输出为”“,而非null  
WriteNullNumberAsZero 数值字段如果为null,输出为0,而非null  
WriteNullBooleanAsFalse Boolean字段如果为null,输出为false,而非null  
SkipTransientField 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true  
SortField 按字段名称排序后输出。默认为false  
WriteTabAsSpecial 把 做转义输出,默认为false 不推荐
PrettyFormat 结果是否格式化,默认为false  
WriteClassName 序列化时写入类型信息,默认为false。反序列化是需用到  
DisableCircularReferenceDetect 消除对同一对象循环引用的问题,默认为false  
WriteSlashAsSpecial 对斜杠’/’进行转义  
BrowserCompatible 将中文都会序列化为uXXXX格式,字节数会多一些,但是能兼容IE 6,默认为false  
WriteDateUseDateFormat 全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);  
DisableCheckSpecialChar 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false  
NotWriteRootClassName 含义  
BeanToArray 将对象转为array输出  
WriteNonStringKeyAsString 含义  
NotWriteDefaultValue 含义  
BrowserSecure 含义  
IgnoreNonFieldGetter 含义  
WriteEnumUsingName 含义  

然后记录你需要的名称(也就是第一列),并修改你的spring-mvc文件:下图中加粗部分 为新加入部分

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!--自动扫描,注解控制器 -->
    <context:component-scan base-package="com.xxxxx.credit.controller"/>

    <!--注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- @ResponseBody乱码问题,将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <beans:constructor-arg value="UTF-8"/>
            </beans:bean>
            <!-- 配置Fastjson支持 -->
            <beans:bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <beans:property name="charset" value="UTF-8"/>
                <beans:property name="supportedMediaTypes">
                    <beans:list>
                        <beans:value>application/json</beans:value>
                        <beans:value>text/html;charset=UTF-8</beans:value>
                    </beans:list>
                </beans:property>
                <beans:property name="features">
                    <beans:list>
                        <!-- 是否输出值为null的字段,默认为false-->
                        <beans:value>WriteMapNullValue</beans:value>
                        <beans:value>QuoteFieldNames</beans:value>
                        <beans:value>WriteDateUseDateFormat</beans:value>
                        <beans:value>WriteEnumUsingToString</beans:value>
                    </beans:list>
                </beans:property>
            </beans:bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


    <mvc:interceptors>
        <!--  使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求   -->
        <!-- <bean class="com.bybo.aca.web.interceptor.Login"/> -->
        <mvc:interceptor>
            <!-- 进行拦截:/**表示拦截所有controller -->
            <mvc:mapping path="/**" />
            <bean class="com.xxxxx.credit.interceptor.WebInterceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <!-- 进行拦截:/**表示拦截所有controller -->
            <mvc:mapping path="/**" />
            <bean class="com.xxxxx.credit.interceptor.AccessLogInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <!--静态资源映射-->
    <!--本项目把静态资源放在了WEB-INF的statics目录下,资源映射如下-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
    <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    <bean id="defaultViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/><!--设置JSP文件的目录位置-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- springMvc文件上传需要配置的节点-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20971500"/><!--设置JSP文件的目录位置-->
        <property name="defaultEncoding" value="UTF-8"/><!--设置默认编码-->
        <property name="resolveLazily" value="true"/><!--启用是为了推迟文件解析,以便捕获文件大小异常-->
    </bean>




</beans>

-------------------------------------------------------------------------------------------------------分割线-------------------------------------------------------------------------------------------------------------------------

另外 在解析对方响应的数据时碰到类似这种情况:

{"a_id":"111","b_id":"2222"} 如果你实体类中定义的属性是a_id 这当然可以解析,但如果你再将实体类 parseJson之后就会出现将类中的属性解析为aId,bId

也就是说fastjson会将你的属性名中的下划线取消掉,这时如果还想包含下划线,则应该在该类中的字段上加上:

@JSONField(name="hit_rules")
private String hit_rules;

 -------------------------------------------------------------------------------2017/09/28更新 顺便祝自己生日快乐---------------------------------------------------------------------------------------------------------------------------------------------------------

另外,一些说明是 也同样可以加在属性的get set 方法上 (这样做就不需要加在属性上了):

加在get方法上的注解 用来解析json数据

加在set方法上的注解用来写入数据

不积跬步无以至千里
原文地址:https://www.cnblogs.com/showme1942/p/7596713.html