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;//備注信息

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM