1. 程式人生 > >Spring Boot入門之使用第三方JSON框架

Spring Boot入門之使用第三方JSON框架

筆記摘要

本文以Spring Boot使用fastjson替代原有JSON框架為例,介紹Spring Boot如何使用第三方JSON框架。

Spring Boot使用自定義JSON框架方法

方法一

  1. 在pom.xml中引入相關依賴;
  2. 在App類中繼承WebMvcConfigurerAdapter並重載configureMessageConverters方法,增加自定義的JSON解析框架。

方法二

  1. 在pom.xml中引入相關依賴;
  2. 使用@Bean引入第三方框架。

實驗過程

實驗過程一:

Created with Raphaël 2.1.2
開始配置pom.xml檔案,引入依賴包在App類中繼承WebMvcConfigurerAdapter方法過載configureMessageConverters方法編寫GetJsonDemo類用於生成JSON字串修改MyController類,增加getDemo方法結束

實驗過程二:

Created with Raphaël 2.1.2開始配置pom.xml檔案,引入依賴包在App類中注入Bean:HttpMessageConverters編寫GetJsonDemo類用於生成JSON字串修改MyController類,增加getDemo方法結束

實驗程式碼

pom.xml

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

方法一App類程式碼

package my.spring.helloworld;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework
.boot.autoconfigure.SpringBootApplication; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class App extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 1.定義convert,轉換訊息的物件; * 2.新增fastjson配置資訊; * 3.在convert中新增配置資訊; * 4.將convert新增到converters。 */ FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastJsonHttpMessageConverter); } public static void main( String[] args ) { SpringApplication.run(App.class, args); } }

方法二App類程式碼

package my.spring.helloworld;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
public class App
{
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);
    }

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

GetJsonDemo類程式碼

package my.spring.helloworld;

import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;

public class GetJsonDemo {
    private int id;
    private String name;
    @JSONField(format="yy-MM-dd HH:mm")
    private Date createTime;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

MyController類程式碼

    @RequestMapping("/getJsonDemoData")
    @ResponseBody
    public GetJsonDemo getDemo() {
        GetJsonDemo demo = new GetJsonDemo();
        demo.setId(1);
        demo.setName("JsonDemoData");
        demo.setCreateTime(new Date());
        return demo;
    }

實驗結果

訪問http://localhost:8080/getJsonDemoData,瀏覽器反饋
{ “createTime”:”18-03-06 20:48”, “id”:1, “name”:”JsonDemoData” }