1. 程式人生 > >Spring Boot學習之旅:(四)springboot 整合 fastjson

Spring Boot學習之旅:(四)springboot 整合 fastjson

springboot 預設使用的 jackson 但是聽說某寶的fastjson 效能很好,而且平時用的習慣,所以來整合一下。
首先在pom 中匯入依賴

  <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15  </version>
    </dependency>
 ```
 匯入後當然還需要配置一下啦下面介紹一下配置的方式,以及簡單的使用fastjson。
 第一種建立一個fastjson的配置類繼承WebMvcConfigurerAdapter 將fastjson配置新增到Converters 

@Configuration
public class FaJsonConfig extends WebMvcConfigurerAdapter{
public void configureMessageConverters(List> converters) {
/**1 預先定義一個Converters 轉換訊息的物件
* 2 新增fastjson的配置資訊
* 3 早converter中新增配置資訊
* 4.將converter新增到converters中
*/
super.configureMessageConverters(converters);//
FastJsonHttpMessageConverter fastConstructor=new FastJsonHttpMessageConverter();//1
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//2
fastConstructor.setFastJsonConfig(fastJsonConfig);//3
converters.add(fastConstructor);//4
}
}

//或者宣告成一個bean 程式碼如下


@Configuration
public class FaJsonConfig extends WebMvcConfigurerAdapter{
    /*public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        *//**1  預先定義一個Converters 轉換訊息的物件
         * 2 新增fastjson的配置資訊
         * 3 早converter中新增配置資訊
         * 4.將converter新增到converters中
         */
/* super.configureMessageConverters(converters);// FastJsonHttpMessageConverter fastConstructor=new FastJsonHttpMessageConverter();//1 FastJsonConfig fastJsonConfig=new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//2 fastConstructor.setFastJsonConfig(fastJsonConfig);//3 converters.add(fastConstructor);//4 }*/ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }

那麼這時候在實體類中使用@JSONField(serialize=false),是不是此欄位就不返回了,如果是的話,那麼恭喜你配置成功了。
下面建立兩個類簡要說下fastjson的簡單使用
usermodel


public class User{
    private int id;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;//建立時間
    @JSONField(serialize = false)  
    private String name;
    @JSONField(deserialize = false)  
    private int age;
    @JSONField(name = "list")
    private List<Address> addrList;

    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List<Address> getAddrList() {
        return addrList;
    }
    public void setAddrList(List<Address> addrList) {
        this.addrList = addrList;
    }

    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

address model

public class Address{
    /*
     * 省份
     */
    private String province;
    /*
     * 城市
     */
    private String city;
    /*
     * 地區
     */
    private String area;
    /*
     * 具體詳細地址
     */
    private String detail;
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getArea() {
        return area;
    }
    public void setaArea(String area) {
        this.area = area;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }

}

準備一個測試用的json字串

{“addrList”:[{“city”:”某市”,”detail”:”某大學”,”area”:”某區”,”province”:”某省”},{“city”:”某某市”,”detail”:”某某大學”,”area”:”某某區”,”province”:”某某省”}],”age”:22,”id”:1,”name”:”cxhc”}

1、反序列化

    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
        System.out.println( "Hello World" );
        String jsonString="{'addrList':[{'city':'某市','detail':'某大學','area':'某區','province':'某省'},{'city':'某某市','detail':'某某大學','area':'某某區','province':'某某省'}],'age':22,'id':1,'name':'cxhc'}";
        User user = JSON.parseObject(jsonString, User.class);
        System.out.println(user);

    }

得到輸出結果:
User [id=1, name=cxhc, age=22, addrList=[Address [province=某省, city=某市, area=null, detail=某大學], Address [province=某某省, city=某某市, area=null, detail=某某大學]]]
2、序列化

public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
        System.out.println( "Hello World" );
        User user=new User();
        user.setName("cxhc");
        user.setCreateTime(new Date());
        user.setAge(11);
        user.setId(1);
        List <Address> list=new ArrayList<Address>();
        Address address=new Address();
        address.setaArea("某區");
        address.setCity("某市");
        address.setDetail("某大學");
        address.setProvince("某省");
        list.add(address);
        address.setaArea("某某區");
        address.setCity("某某市");
        address.setDetail("某某大學");
        address.setProvince("某某省");
        list.add(address);
        user.setAddrList(list);
        System.out.println(JSON.toJSONString(user));
    }

//得到如下結果
{“addrList”:[{“city”:”某市”,”detail”:”某大學”,”province”:”某省”},{“city”:”某某市”,”detail”:”某某大學”,”province”:”某某省”}],”age”:22,”id”:1,”name”:”cxhc”}
3、JSONField

package com.alibaba.fastjson.annotation;
public @interface JSONField {
// 配置序列化和反序列化的順序,1.1.42版本之後才支援
int ordinal() default 0;
// 指定欄位的名稱
String name() default “”;
// 指定欄位的格式,對日期格式有用
String format() default “”;
// 是否序列化
boolean serialize() default true;
// 是否反序列化
boolean deserialize() default true;
}
3.1 指定欄位序列化名稱
例如

@JSONField(name = "list")
 private List<Address> addrList;

3.2格式化

  @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;//建立時間

3.3 序列化的時候不被序列化
@JSONField(serialize = false)

3.4 反序列化的時候 不被反序列化
@JSONField(deserialize = false)