• <ol id="gebcs"></ol>
    <track id="gebcs"><i id="gebcs"></i></track>

    SpringBoot使用fastJson代替JackSon

    時間:2022-09-08 09:46:27 類型:JAVA
    字號:    

      方法一:最安全做法使用bean替代默認轉換器方法

    寫法一

    @Configuration
    public class MyConfiguration {
        @Bean
        public HttpMessageConverters customConverters() {
            List<HttpMessageConverters> converters = new ArrayList<>();
            // 1、需要先定義一個convert轉換消息的對象;
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            // 2、添加fastjson的配置信息,比如 是否要格式化返回json數據
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            // 3、在convert中添加配置信息.
            fastConverter.setFastJsonConfig(fastJsonConfig);
            // 4、將convert添加到converters當中.
            converters.add(fastConverter);
            return new HttpMessageConverters(true, converters);
        }
    
    }

      //寫法二

    @Configuration
    public class FastJsonHttpMessageConfig {
    	@Bean
    	public HttpMessageConverters fastJsonHttpMessageConverters() {
    		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    
    		FastJsonConfig fastJsonConfig = new FastJsonConfig();
    		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
    				SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
    				SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse);
    
    		fastJsonConfig.setFeatures(Feature.OrderedField);
    		List<MediaType> fastMedisTypes = new ArrayList<>();
    		fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
    		fastConverter.setSupportedMediaTypes(fastMedisTypes);
    
    		fastConverter.setFastJsonConfig(fastJsonConfig);
    		HttpMessageConverter<?> converter = fastConverter;
    		return new HttpMessageConverters(converter);
    	}
    }

      方法二

      第三種實現父類,缺點很明顯,完全接管的springmvc,默認配置全部失效,需要重新自己配置

    @Configuration
    public class SpringMvcConfigure extends WebMvcConfigurationSupport{
    
        /**
         * 配置消息轉換器
         * @param converters
         */
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            //自定義配置...
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
                    SerializerFeature.WriteEnumUsingToString,
                    /*SerializerFeature.WriteMapNullValue,*/
                    SerializerFeature.WriteDateUseDateFormat,
                    SerializerFeature.DisableCircularReferenceDetect);
            fastJsonHttpMessageConverter.setFastJsonConfig(config);
            converters.add(fastJsonHttpMessageConverter);
            supper.configureMessageConverters(converters);
        }
    }


    黄片基地 <