SpringBoot攻略八、使用fastjson

在启动类@SpringBootApplication添加如下:

/**
* fastjson替换默认的jackjson(springmvc)
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
        
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, 
                                            SerializerFeature.WriteNullListAsEmpty);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        // 处理中文乱码问题
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        
        HttpMessageConverter converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);
}

你可能感兴趣的:(SpringBoot攻略八、使用fastjson)