原因:
1.jackjson無法處理嵌套的entity關係;例:entity:A中包含entity:B,B中又有C;
2.fastjson性能優越;
3.把fastjson用於memached緩存對象數據;
使用方法:
一:配置文件
<!-- 啓用註解 -->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="mappingFastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="mappingFastJsonHttpMessageConverter"
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="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
<property name="serializerFeatures">
<list>
<!-- 輸出key時是否使用雙引號 -->
<value>QuoteFieldNames</value>
<!-- 是否輸出值爲null的字段 -->
<!-- <value>WriteMapNullValue</value> -->
<!-- 數值字段如果爲null,輸出爲0,而非null -->
<value>WriteNullNumberAsZero</value>
<!-- List字段如果爲null,輸出爲[],而非null -->
<value>WriteNullListAsEmpty</value>
<!-- 字符類型字段如果爲null,輸出爲"",而非null -->
<value>WriteNullStringAsEmpty</value>
<!-- Boolean字段如果爲null,輸出爲false,而非null -->
<value>WriteNullBooleanAsFalse</value>
<!-- null String不輸出 -->
<value>WriteNullStringAsEmpty</value>
<!-- null String也要輸出 -->
<!-- <value>WriteMapNullValue</value> -->
<!-- Date的日期轉換器,這個只有默認yyyy-MM-dd HH:mm:ss,使用最上的dateFormat即可修改格式 -->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</property>
</bean>
注:當屬性值爲空的時候,fastjson默認是不輸出的;
代碼註釋:
@JSONField(name = "operator")
private String operation="test";
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
//name:json串:"**operator**:test,createTime:'2019-09-21 15:05:31'"
//ordinal:排序,數字越小越前;
//serialize:若serialize = false,序列化時過濾該字段;
//deserialize:若deserialize = false,反序列化時過濾該字段;
注:fastjson的賦值是用get/set方法,和註解無關;