溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

@JsonProperty,@NotNull,@JsonIgnore怎么使用

發布時間:2022-08-30 11:42:44 來源:億速云 閱讀:225 作者:iii 欄目:開發技術

@JsonProperty, @NotNull, @JsonIgnore怎么使用

在Java開發中,尤其是在使用Spring Boot或Jackson庫進行JSON序列化和反序列化時,注解(Annotations)是非常有用的工具。本文將詳細介紹@JsonProperty、@NotNull@JsonIgnore這三個常用注解的使用方法,并通過示例代碼幫助讀者更好地理解它們的應用場景。

1. @JsonProperty

@JsonProperty是Jackson庫中的一個注解,用于指定JSON屬性與Java對象屬性之間的映射關系。它可以用于字段、方法或構造函數參數上。

1.1 基本用法

@JsonProperty最常見的用法是用于字段或方法上,以指定JSON屬性名與Java對象屬性名之間的映射關系。

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    @JsonProperty("user_name")
    private String userName;

    @JsonProperty("user_age")
    private int userAge;

    // 省略getter和setter方法
}

在上面的例子中,userName字段在序列化為JSON時會被映射為user_name,而userAge字段會被映射為user_age。

1.2 用于方法

@JsonProperty也可以用于getter或setter方法上,以指定JSON屬性名。

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    private String userName;
    private int userAge;

    @JsonProperty("user_name")
    public String getUserName() {
        return userName;
    }

    @JsonProperty("user_name")
    public void setUserName(String userName) {
        this.userName = userName;
    }

    @JsonProperty("user_age")
    public int getUserAge() {
        return userAge;
    }

    @JsonProperty("user_age")
    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }
}

1.3 用于構造函數參數

@JsonProperty還可以用于構造函數參數上,以便在反序列化時正確映射JSON屬性。

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    private String userName;
    private int userAge;

    public User(@JsonProperty("user_name") String userName, @JsonProperty("user_age") int userAge) {
        this.userName = userName;
        this.userAge = userAge;
    }

    // 省略getter和setter方法
}

2. @NotNull

@NotNull是Java Bean Validation(JSR 303)中的一個注解,用于標記一個字段、方法或參數不能為null。它通常用于數據驗證場景中。

2.1 基本用法

@NotNull通常用于字段或方法參數上,以確保在數據驗證時該值不為null。

import javax.validation.constraints.NotNull;

public class User {
    @NotNull
    private String userName;

    @NotNull
    private Integer userAge;

    // 省略getter和setter方法
}

在上面的例子中,userNameuserAge字段被標記為@NotNull,這意味著在數據驗證時,這兩個字段不能為null。

2.2 用于方法參數

@NotNull也可以用于方法參數上,以確保傳入的參數不為null。

import javax.validation.constraints.NotNull;

public class UserService {
    public void createUser(@NotNull String userName, @NotNull Integer userAge) {
        // 業務邏輯
    }
}

2.3 結合Spring Boot使用

在Spring Boot中,@NotNull通常與@Valid注解一起使用,以觸發數據驗證。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class UserController {
    @PostMapping("/users")
    public void createUser(@Valid @RequestBody User user) {
        // 業務邏輯
    }
}

在上面的例子中,@Valid注解會觸發對User對象的數據驗證,如果userNameuserAgenull,則會拋出MethodArgumentNotValidException異常。

3. @JsonIgnore

@JsonIgnore是Jackson庫中的一個注解,用于標記一個字段或方法在序列化和反序列化時被忽略。它通常用于隱藏敏感信息或不需要暴露的字段。

3.1 基本用法

@JsonIgnore通常用于字段或方法上,以指定該字段或方法在序列化和反序列化時被忽略。

import com.fasterxml.jackson.annotation.JsonIgnore;

public class User {
    private String userName;
    private int userAge;

    @JsonIgnore
    private String password;

    // 省略getter和setter方法
}

在上面的例子中,password字段被標記為@JsonIgnore,這意味著在序列化為JSON時,password字段不會被包含在輸出中;在反序列化時,password字段也不會從JSON中讀取。

3.2 用于方法

@JsonIgnore也可以用于getter或setter方法上,以指定該方法在序列化和反序列化時被忽略。

import com.fasterxml.jackson.annotation.JsonIgnore;

public class User {
    private String userName;
    private int userAge;
    private String password;

    @JsonIgnore
    public String getPassword() {
        return password;
    }

    @JsonIgnore
    public void setPassword(String password) {
        this.password = password;
    }

    // 省略其他getter和setter方法
}

3.3 結合@JsonProperty使用

在某些情況下,你可能希望在某些場景下忽略某個字段,而在其他場景下保留它。這時可以結合@JsonProperty@JsonIgnore使用。

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    private String userName;
    private int userAge;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    // 省略getter和setter方法
}

在上面的例子中,password字段在序列化時會被忽略(即不會出現在JSON輸出中),但在反序列化時仍然可以從JSON中讀取。

4. 綜合示例

下面是一個綜合使用@JsonProperty、@NotNull@JsonIgnore的示例。

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;

public class User {
    @NotNull
    @JsonProperty("user_name")
    private String userName;

    @NotNull
    @JsonProperty("user_age")
    private Integer userAge;

    @JsonIgnore
    private String password;

    public User(@JsonProperty("user_name") String userName, @JsonProperty("user_age") Integer userAge) {
        this.userName = userName;
        this.userAge = userAge;
    }

    // 省略getter和setter方法
}

在這個示例中:

  • userNameuserAge字段被標記為@NotNull,并且在序列化和反序列化時分別映射為user_nameuser_age。
  • password字段被標記為@JsonIgnore,在序列化和反序列化時會被忽略。

5. 總結

@JsonProperty、@NotNull@JsonIgnore是Java開發中非常常用的注解,尤其是在處理JSON序列化和反序列化以及數據驗證時。通過合理使用這些注解,可以有效地控制數據的映射、驗證和隱藏,從而提高代碼的可讀性和安全性。

  • @JsonProperty用于指定JSON屬性與Java對象屬性之間的映射關系。
  • @NotNull用于確保字段或參數不為null,通常用于數據驗證。
  • @JsonIgnore用于在序列化和反序列化時忽略某個字段或方法。

希望本文能幫助你更好地理解和使用這些注解,并在實際開發中靈活運用它們。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女