jackSon註解-針對返回的實體屬性

jackSon註解– @JsonInclude 註解不返回null值字段

@Data
@JsonInclude(value= JsonInclude.Include.NON_NULL)
public class DraftBoxTStoreUserDto extends TStoreUser{

    private String entryChannelsStr;

    private String accountCharacterStr;

    private String marriageStr;

    private String educationStr;

    private String prefix;

    private TStoreUserEnclosure tStoreUserEnclosureList;

    private List<TStoreUserWork> tStoreUserWorkList;

    private List<TStoreUserFamily>  tStoreUserFamilyList;

    private List<TStoreUserUrgent>  tStoreUserUrgentList;
}

@JsonInclude(JsonInclude.Include.NON_NULL)表示,如果值爲null,則不返回

@JsonInclude(value= JsonInclude.Include.ALWAYS)表示,全部屬性的字段值正常返回

 

全局jsckson配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1/root?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
  jackson:
    default-property-inclusion: non_null # 全局jackson配置

JSON庫 Jackson 常用註解介紹

注:以下所涉及到的實體類都使用了 Lomback 插件

Jackson JSON 框架中包含了大量的註解來讓我們可以干預 Jackson 的 JSON 處理過程,
例如我們可以通過註解指定 java pojo 的某些屬性在生成 json 時被忽略。。本文主要介紹如何使用 Jackson 提供的註解。
Jackson註解主要分成三類,一是隻在序列化時生效的註解;二是隻在反序列化時候生效的註解;三是兩種情況下都生效的註解。

一: 兩種情況下都有效的註解 

1. @JsonIgnore 作用域屬性或方法上

@JsonIgnore 用來告訴 Jackson 在處理時忽略該註解標註的 java pojo 屬性,
不管是將 java 對象轉換成 json 字符串,還是將 json 字符串轉換成 java 對象。

@Data
public class SellerInfoEntity {

    private String id;
    private String username;
    private String password;
    private String openid;

    @JsonIgnore
    private Timestamp createTime;
    @JsonIgnore
    private Timestamp updateTime;


    public SellerInfoEntity() {
    }

    public SellerInfoEntity(String id, String username, String password, String openid) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.openid = openid;
    }
}

2. @JsonIgnoreProperties 作用在類上

@JsonIgnoreProperties 和 @JsonIgnore 的作用相同,都是告訴 Jackson 該忽略哪些屬性,
不同之處是 @JsonIgnoreProperties 是類級別的,並且可以同時指定多個屬性。

@Data
@JsonIgnoreProperties(value = {"createTime","updateTime"})
public class SellerInfoEntity {

    private String id;
    private String username;
    private String password;
    private String openid;

    private Timestamp createTime;
    private Timestamp updateTime;


    public SellerInfoEntity() {
    }

    public SellerInfoEntity(String id, String username, String password, String openid) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.openid = openid;
    }
}

使用Spring Boot快速搭建Controller進行測試:

@RestController
@RequestMapping("/jackson")
public class TestJackson {



    @RequestMapping("test1")
    public Result test1(){

        SellerInfoEntity entity = new SellerInfoEntity("1","user1","123456","openid");

        return new Result(MyResultEnum.SUCCESS,entity);

    }





}

訪問效果:

使用註解前:返回值

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "password": "123456",
        "openid": "openid",
        "createTime": null,
        "updateTime": null
    }
}

使用註解後:返回值

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "password": "123456",
        "openid": "openid",
    }
}

3. @JsonIgnoreType

@JsonIgnoreType 標註在類上,當其他類有該類作爲屬性時,該屬性將被忽略。

package org.lifw.jackosn.annotation;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class SomeOtherEntity {
    private Long id;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}
public class SomeEntity {
    private String name;
    private String desc;
    private SomeOtherEntity entity;
}

SomeEntity 中的 entity 屬性在json處理時會被忽略。

4. @JsonProperty


@JsonProperty 可以指定某個屬性和json映射的名稱。例如我們有個json字符串爲{“user_name”:”aaa”},
而java中命名要遵循駝峯規則,則爲userName,這時通過@JsonProperty 註解來指定兩者的映射規則即可。這個註解也比較常用。

public class SomeEntity {
    @JsonProperty("user_name")
    private String userName;
      // ...
}

二、只在序列化情況下生效的註解

1. @JsonPropertyOrder

在將 java pojo 對象序列化成爲 json 字符串時,使用 @JsonPropertyOrder 可以指定屬性在 json 字符串中的順序。

2. @JsonInclude

在將 java pojo 對象序列化成爲 json 字符串時,使用 @JsonInclude 註解可以控制在哪些情況下才將被註解的屬性轉換成 json,例如只有屬性不爲 null 時。

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SellerInfoEntity {

    private String id;
    private String username;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String password;
    private String openid;

    private Timestamp createTime;
    private Timestamp updateTime;


    public SellerInfoEntity() {
    }

    public SellerInfoEntity(String id, String username, String password, String openid) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.openid = openid;
    }
}

Controller 測試

@RestController
@RequestMapping("/jackson")
public class TestJackson {

    @RequestMapping("test1")
    public Result test1(){

        SellerInfoEntity entity = new SellerInfoEntity("1","user1","","openid");

        return new Result(MyResultEnum.SUCCESS,entity);

    }

}

結果:

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "openid": "openid"
    }
}

上述例子的意思是 SellerInfoEntity 的所有屬性只有在不爲 null 的時候才被轉換成 json,
如果爲 null 就被忽略。並且如果password爲空字符串也不會被轉換.

該註解也可以加在某個字段上。

另外@JsonInclude註解還有很多其它的範圍,例如 NON_EMPTY、NON_DEFAULT等

注:@JsonInclude可作用在類上,可作用在屬性上

三、是在反序列化情況下生效的註解

1. @JsonSetter

@JsonSetter 標註於 setter 方法上,類似 @JsonProperty ,也可以解決 json 鍵名稱和 java pojo 字段名稱不匹配的問題。

public class SomeEntity {
    private String desc;
    @JsonSetter("description")
    public void setDesc(String desc) {
        this.desc = desc;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章