1 pom.xml 中添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
2 配制 FastJson --->创建FastJsonConfiguration
@Configuration
public class FastJsonConfiguration extends WebMvcConfigurerAdapter {
/**
* 自定义消息转换器
*
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.构建了一个HttpMessageConverter FastJson 消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.定义一个配置,设置编码方式,和格式化的形式
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//3.设置成了PrettyFormat格式
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false
SerializerFeature.WriteNullBooleanAsFalse,//Boolean字段如果为null,输出为false,而非null
SerializerFeature.WriteNullListAsEmpty,//List字段如果为null,输出为[],而非nul
SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false
SerializerFeature.WriteNullStringAsEmpty//字符类型字段如果为null,输出为"",而非null
);
//4.处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//5.将fastJsonConfig加到消息转换器中
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}