pom
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
Fastjson的SerializerFeature序列化屬性
- PrettyFormat---------結(jié)果是否格式化,默認(rèn)為false
- QuoteFieldNames———-輸出key時(shí)是否使用雙引號(hào),默認(rèn)為true
- WriteMapNullValue——–是否輸出值為null的字段,默認(rèn)為false
- WriteNullNumberAsZero—-數(shù)值字段如果為null,輸出為0,而非null
- WriteNullListAsEmpty—–List字段如果為null,輸出為[],而非null
- WriteNullStringAsEmpty—字符類(lèi)型字段如果為null,輸出為”“,而非null
- WriteNullBooleanAsFalse–Boolean字段如果為null,輸出為false,而非null
- WriteDateUseDateFormat-Date字段按字符串輸出
配置類(lèi)
package com.ghgcn.chapter01.config;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.nio.charset.Charset;
import java.util.List;
@Configuration
public class CustomMVCConfiguration extends WebMvcConfigurerAdapter {
/**
* 配置 FastJsonHttpMessageConverter
* @return
*/
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(Charset.defaultCharset());
//可變參數(shù)幸撕,傳多個(gè)
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.BrowserCompatible,
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteDateUseDateFormat);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setFastJsonConfig(fastJsonConfig);
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//添加FastJsonHttpMessageConverter
converters.add(fastJsonHttpMessageConverter());
}
}
{
"message": "執(zhí)行成功",
"object": {
"birthday": "2018-04-23 17:00:23", //日期有格式化 為指定格式
"gender": null,//null值有輸出
"id": 123,
"name": null
},
"status": 200
}