springmvc集成fastjson

Author Avatar
丁起男 08月 08,2021
  • 在其它设备中阅读本文章

springmvc集成fastjson

springmvc框架中的默认json序列化工具是jackson

我们需要配置消息转换器,将jackson切换到fastjson

方式一

    @Bean
    public HttpMessageConverters httpMessageConverters(){
        //fastjson工具中的消息转换器类
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        //添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //设置全局序列化策略
        SerializerFeature[] serializerFeatures = new SerializerFeature[]{
                //输出key时包含双引号
                //SerializerFeature.QuoteFieldNames,
                //输出空字段
                //SerializerFeature.WriteMapNullValue,
                //数值为null输出0
                //SerializerFeature.WriteNullNumberAsZero,
                //list为null输出[]
                //SerializerFeature.WriteNullListAsEmpty,
                //字符串为null输出""
                //SerializerFeature.WriteNullStringAsEmpty,
                //boolean为null输出false
                //SerializerFeature.WriteNullBooleanAsFalse
        };
        fastJsonConfig.setSerializerFeatures(serializerFeatures);
        //设置编码
        fastJsonConfig.setCharset(Charset.forName("utf-8"));

        //将配置信息添加到消息转换器类
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        //将消息转换类添加到converters中
        HttpMessageConverter httpMessageConverter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(httpMessageConverter);
    }

方式二

@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 使用fastjson替代jackson
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //删除原本的jackson转换器
        for (int i=converters.size()-1; i>=0; i--){
            if (converters.get(i) instanceof MappingJackson2HttpMessageConverter){
                converters.remove(i);
            }
        }
        //创建fastjson转换器
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //自定义配置
        FastJsonConfig config = new FastJsonConfig();
        //全局策略
        config.setSerializerFeatures(
                //输出key时包含双引号
                //SerializerFeature.QuoteFieldNames,
                //输出空字段
                //SerializerFeature.WriteMapNullValue,
                //数值为null输出0
                //SerializerFeature.WriteNullNumberAsZero,
                //list为null输出[]
                //SerializerFeature.WriteNullListAsEmpty,
                //字符串为null输出""
                //SerializerFeature.WriteNullStringAsEmpty,
                //boolean为null输出false
                //SerializerFeature.WriteNullBooleanAsFalse
        );
        converter.setFastJsonConfig(config);
        //添加支持的MediaTypes,默认为“/”,也就是支持全部
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        //添加到mvc中
        converters.add(converter);
    }
}