一、添加依赖关系
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
二、自定义HttpMessageConverter
@Configuration
public class MyFastJsonConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd"); //日期格式
config.setCharset(Charset.forName("UTF-8")); //数据编码
config.setSerializerFeatures(
SerializerFeature.WriteClassName, //是否在生成的JSON中输出类名
SerializerFeature.WriteMapNullValue, //是否输出value为null的数据
SerializerFeature.PrettyFormat,//生成的JSON格式化
SerializerFeature.WriteNullListAsEmpty,//空集合输出[]而非null
SerializerFeature.WriteNullStringAsEmpty//空字符窜输出""而非null
);
converter.setFastJsonConfig(config);
return converter;
}