spring boot (二):使用fastJson解析json数据

如果我们想在spring boot中使用第三方的json解析框架:

1)我们需要在pom.xml文件中引入第三方包的依赖;

2)实现方法:

方法1 需要在启动类中继承WebMvcConfigurerAdapter 类,并重写该类的configureMessageConverters方法。

方法2. 我们直接使用@Bean注入第三方的 解析框架。

1、引入fastJson的依赖库

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

在1.2.10版本后会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConvert,支持4.2以下版本;

一个是FastJsonHttpMessageConvert4,支持4.2以上版本。

2、配置fastJson(支持两种方法)

(1) 方法一:

  <1> 启动类继承 WebMvcConfigurerAdapter 类;

  <2> 覆盖方法 configureMessageConverters;

①  启动类代码如下:

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
    {
        super.configureMessageConverters(converters);
        /*
         * 1、需要先定义一个 convert 转换消息对象;
         * 2、添加 fastJson 的配置信息,比如: 是否要格式化返回的Json数据;
         * 3、在 Convert 中添加配置信息;
         * 4、将 convert 添加到 converts 中;
         */

        //1、需要先定义一个 convert 转换消息对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加 fastJson 的配置信息,比如: 是否要格式化返回的Json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        //3、在 Convert 中添加配置信息;
        fastConverter.setFastJsonConfig(fastJsonConfig);

        //4、将 convert 添加到 converts 中;
        converters.add(fastConverter);

    }

    public static void main(String[] args)
    {
        /*
         * 在main方法中进行启动App类的应用程序
         */
        SpringApplication.run(App.class, args);
    }
}

② Controller的接口代码如下:

@RestController
public class HelloController
{/**
     * 返回的JSON数据
     * @return
     */
    @RequestMapping("/getDemo")
    public Demo getDemo()
    {
        Demo demo = new Demo();
        demo.setId(1);
        demo.setName("zhangsan");
        demo.setCreateTime(new Date());
        return demo;
    }
}

③ 测试代码:

在Demo实体类中的createTime属性,使用 @JSONField 注解格式化Date类型为"yyyy-MM-dd HH:mm:ss"的格式;如下所示:

public class Demo
{
    private int id;
    private String name;

    //注解使用的包: com.alibaba.fastjson.annotation.JSONField
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;//创建时间

    //getter 和 setter 方法省略
}

在浏览器中打开 http://localhost:8080/getDemo :

若返回的json数据中 "createTime" 的格式为:yyyy-MM-dd HH:mm:ss,则表示使用的是配置的fastJson处理的数据,否则表示的配置的fastJson不生效。

(2) 方法二:

  <1> 在启动类中注入Bean:HttpMessageConverters

启动类的代码如下:

@SpringBootApplication
public class App
{
    /**
     * 使用 @Bean 注入 FastJsonHttpMessageConverter
     * @return
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters()
    {
        //1、需要先定义一个 convert 转换消息对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加 fastJson 的配置信息,比如: 是否要格式化返回的Json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        //3、在 Convert 中添加配置信息;
        fastConverter.setFastJsonConfig(fastJsonConfig);

        //4、
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

    public static void main(String[] args)
    {
        /*
         * 在main方法中进行启动App类的应用程序
         */
        SpringApplication.run(App.class, args);
    }
}

 3、 fastJson的一些使用方法

 (1)格式化日期格式:

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;//创建时间

(2)若不想让接口返回一个属性,则设置 @JSONFieldserialize 属性为false(不进行序列化);

    /*
     * 不想返回该属性, serialize:是否进行序列化
     */
    @JSONField(serialize = false)
    private String remarks;//备注信息
原文地址:https://www.cnblogs.com/yufeng218/p/7124049.html