springboot(15)修改HTTP默认序列化工具

使用fastjson替代springboot默认的序列化工具

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class FormatConfig {

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}

踩坑记录:

使用注解@EnableWebMvc之后,该注解会使用jackson作为默认序列化工具,导致以上代码失效!!!!