十七、springboot配置FastJson为Spring Boot默认JSON解析框架

前提

  springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用

1.引入fastjson依赖库:

  maven:

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
</dependencies>

  gradle:

compile("com.alibaba:fastjson:${fastJsonVersion}")
ext {
        fastJsonVersion    = '1.2.46'
    }

  注: 这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+

2.在启动类中配置

2.1配置方式一(通过继承的方式)

  1、启动类继承WebMvcConfigurerAdapter
  2、重写configureMessageConverters方法

@SpringBootApplication
@EnableDiscoveryClient
@EnableScheduling
public class MemberApplication extends WebMvcConfigurerAdapter {
    /**
     * 配置FastJson为方式一
     * @return*/
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        /*
         * 1、需要先定义一个convert转换消息的对象 2、添加fastJson的配置信息,比如:是否要格式化返回json数据 3、在convert中添加配置信息
         * 4、将convert添加到converters当中
         * 
         */
        // 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);
    }

    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }

}

  注:开发中为了统一管理配置,可以放入配置类中,启动类只做启动的功能

@Configuration
public class HttpConverterConfig {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1.定义一个converters转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 3.在converter中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 4.将converter赋值给HttpMessageConverter
        HttpMessageConverter<?> converter = fastConverter;
        // 5.返回HttpMessageConverters对象
        return new HttpMessageConverters(converter);
    }
}

2.2配置方式二(通过@Bean注入的方式

  在App.java启动类中,注入Bean : HttpMessageConverters

@SpringBootApplication
@EnableDiscoveryClient
@EnableScheduling
public class MemberApplication {
    /**
     * 配置FastJson方式二
     * @return  HttpMessageConverters
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1.定义一个converters转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 3.在converter中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 4.将converter赋值给HttpMessageConverter
        HttpMessageConverter<?> converter = fastConverter;
        // 5.返回HttpMessageConverters对象
        return new HttpMessageConverters(converter);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }

}

  在pojo类中:

    private int id;
    private String name;
    
    //com.alibaba.fastjson.annotation.JSONField
    @JSONField(format="yyyy-MM-dd HH:mm")
    private Date createTime;//创建时间.
    
    /*
     * serialize:是否需要序列化属性.
     */
    @JSONField(serialize=false)
    private String remarks;//备注信息.

那么这时候在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的话,那么恭喜你配置成功了,其中JSONField的包路径是:com.alibaba.fastjson.annotation.JSONField。

参考:

  http://www.cnblogs.com/xujie09/p/8461483.html

  https://blog.csdn.net/xuqingge/article/details/53561529

原文地址:https://www.cnblogs.com/soul-wonder/p/9052422.html