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

发布时间:2022-08-30 11:42:44 作者:iii
来源:亿速云 阅读:215

@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方法
}

在这个示例中:

5. 总结

@JsonProperty@NotNull@JsonIgnore是Java开发中非常常用的注解,尤其是在处理JSON序列化和反序列化以及数据验证时。通过合理使用这些注解,可以有效地控制数据的映射、验证和隐藏,从而提高代码的可读性和安全性。

希望本文能帮助你更好地理解和使用这些注解,并在实际开发中灵活运用它们。

推荐阅读:
  1. 用jackson的@JsonProperty注解属性名,会多出一个字段
  2. jsonFiled与jsonproperty

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

@jsonproperty @notnull

上一篇:怎么用vue+echars封装气泡图

下一篇:Spring是如何扩展解析xml接口的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》