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 把\t做轉義輸出,默認為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方法上的注解用來寫入數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM