springboot使用fastjson

1、spring boot默认json解析框架是jackson

2、引入fastjson依赖:必须是1.2.10以上版本才支持


   com.alibaba
   fastjson
   1.2.15

3、编写测试实体,若不返回@JSONField(serialize= false)注解的属性则表示使用fastjson解析}

方法一:

         a、启动类继承extends WebMvcConfigurer

         b、覆盖方法configureMessageConverters

@Override
public void configureMessageConverters(List> converters) {
   super.configureMessageConverters(converters);

   FastJsonHttpMessageConverter oFastConverter = new FastJsonHttpMessageConverter();

   FastJsonConfig oFastJsonConfig = new FastJsonConfig();
   oFastJsonConfig.setSerializerFeatures(
         SerializerFeature.PrettyFormat
   );
   oFastConverter.setFastJsonConfig(oFastJsonConfig);
   //处理中文乱码问题
   List oFastMediaTypeList = new ArrayList<>();
   oFastMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
   oFastConverter.setSupportedMediaTypes(oFastMediaTypeList);

   converters.add(oFastConverter);
}

         方法二:配置类中注入Bean : HttpMessageConverters

@Configuration
public class MyBean {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter oFastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig oFastJsonConfig = new FastJsonConfig();
        oFastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        oFastConverter.setFastJsonConfig(oFastJsonConfig);
        //处理中文乱码问题
        List oFastMediaTypeList = new ArrayList<>();
        oFastMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        oFastConverter.setSupportedMediaTypes(oFastMediaTypeList);

        HttpMessageConverter oConverter = oFastConverter;
        return new HttpMessageConverters(oConverter);
    }
}

你可能感兴趣的:(springboot使用fastjson)