Jackson @JsonIgnore、@JsonIgnoreProperties 和@JsonIgnoreType将对象序列化为 JSON 时忽略某些字段

在本教程中,我将通过一个示例向您展示如何在使用 Jackson @JsonIgnore、@JsonIgnoreProperties 和 @JsonIgnoreType注释将对象序列化为 JSON 时忽略某些字段。这些注解用于忽略 JSON 序列化和反序列化中的逻辑属性。
  • @JsonIgnore用于忽略序列化和反序列化中使用的逻辑属性。@JsonIgnore 可用于 setter、getter 或字段。
  • @JsonIgnoreProperties忽略 JSON 序列化和反序列化中的指定逻辑属性。它在类级别进行了注释。
  • @JsonIgnoreType在类级别进行了注释,它忽略了整个类。
当 Jackson 的默认值不够并且我们需要准确控制序列化为 JSON 的内容时,这非常有用。让我们通过示例来演示这些注解的用法。

1.Maven依赖

我们首先在 pom.xml 中添加以下依赖项:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

此依赖项还会将以下库传递到类路径中:
  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar
始终使用 Maven 中央存储库中的最新版本进行 Jackson 数据绑定。
让我们通过示例演示如何在使用 Jackson 将对象序列化为 JSON 时忽略某些字段。

2. 使用@JsonIgnoreProperties 在类级别忽略字段

我们可以在类级别忽略特定字段,使用@JsonIgnoreProperties注释并指定字段:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {
    "id",
    "firstName"
})
public class CustomerDTO {
    private final String id;
    private final String firstName;
    private final String lastName;

    public CustomerDTO(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

让我们用main()方法测试上面的代码,注意在对象被写入 JSON 之后,该字段确实不是输出的一部分:

package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IgnoreFieldTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");

        String dtoAsString = mapper.writeValueAsString(dtoObject);

        System.out.println(dtoAsString);
    }
}

输出:
{"lastName":"Stark"}
请注意,我们忽略了CustomerDTO的两个字段“id”“firstName” ,只打印了“lastName”

3. 使用@JsonIgnore 在字段级别忽略字段

我们也可以直接通过字段上的@JsonIgnore注解直接忽略字段:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class CustomerDTO {

    @JsonIgnore
    private final String id;

    @JsonIgnore
    private final String firstName;
    private final String lastName;

    public CustomerDTO(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

让我们用main()方法测试上面的代码,注意在对象被写入 JSON 之后,该字段确实不是输出的一部分:

package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IgnoreFieldTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");

        String dtoAsString = mapper.writeValueAsString(dtoObject);

        System.out.println(dtoAsString);
    }
}

输出:

{"lastName":"Stark"}
请注意,我们使用@JsonIgnore注释忽略了CustomerDTO的两个字段“id”“firstName” ,并且只打印了“lastName”

4. 使用@JsonIgnoreType 按类型忽略所有字段

我们还可以使用@JsonIgnoreType注释 忽略指定类型的所有字段。如果我们控制类型,那么我们可以直接对类进行注解:
@JsonIgnoreType
public static class Name {
    public String firstName;
    public String lastName;
    public Name(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

这是完整的代码:

package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnoreType;

public class UserDTO {

    public int id;
    public Name name;


    public UserDTO(int id, Name name) {
        super();
        this.id = id;
        this.name = name;
    }

    @JsonIgnoreType
    public static class Name {
        public String firstName;
        public String lastName;
        public Name(String firstName, String lastName) {
            super();
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
}

让我们用main()方法测试上面的代码,注意在对象被写入 JSON 之后,该字段确实不是输出的一部分:

package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonIgnoreTypeTest {

    public static void main(String[] args) throws JsonProcessingException {
        UserDTO.Name name = new UserDTO.Name("John", "Doe");
        UserDTO user = new UserDTO(1, name);

        String result = new ObjectMapper()
            .writeValueAsString(user);

        System.out.println(result);
    }
}

输出:

{"id":1}
请注意,使用@JsonIgnoreType注释会忽略 Name 类字段。

相关文章

GitHub 存储库

本文的源代码可在我的 GitHub 存储库中找到, 网址为GitHub - RameshMF/jackson-json-tutorial: Tutorial and examples of Jackson APIs
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值