Jackson - 序列化时更改字段名称

在这个简短的教程中,我将向您展示如何在序列化时更改字段名称以映射到另一个JSON属性。

Jackson库提供了@JsonProperty注解,用于改变序列化JSON中的属性名称。

依赖项

首先,在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中央仓库中的最新版本。

更改字段名进行序列化

1. 不使用@JsonProperty注解

我们先创建一个简单的Java类,并测试它而不添加@JsonProperty注解。

User.java

package net.javaguides.jackson.annotations;

public class User {
    public int id;
    private String firstName;
    private String lastName;
    private String fullName;

    public User(int id, String firstName, String lastName, String fullName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = fullName;
    }

    // Getters and Setters
}

使用主方法测试上述代码:

JsonPropertyAnnotationTest.java

package net.javaguides.jackson.annotations;

import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonPropertyAnnotationTest {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
        String result = mapper.writeValueAsString(bean);
        System.out.println(result);
    }
}

输出结果如下:

{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

如你所见,如果不使用@JsonProperty注解,那么属性名将与类中的getter和setter方法相同。

2. 使用@JsonProperty注解

现在让我们给User类的字段添加@JsonProperty注解,来自定义输出,使得JSON格式如下所示:

{
  "id" : 1,
  "first_name" : "Ramesh",
  "last_name" : "Fadatare",
  "full_name" : "Ramesh Fadatare"
}

User.java (带@JsonProperty注解)

package net.javaguides.jackson.annotations;

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    public int id;

    @JsonProperty("first_name")
    private String firstName;

    @JsonProperty("last_name")
    private String lastName;

    @JsonProperty("full_name")
    private String fullName;

    public User(int id, String firstName, String lastName, String fullName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = fullName;
    }

    // Getters and Setters
}

再次使用主方法测试修改后的代码:

JsonPropertyAnnotationTest.java

package net.javaguides.jackson.annotations;

import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonPropertyAnnotationTest {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
        String result = mapper.writeValueAsString(bean);
        System.out.println(result);
    }
}

输出结果如下:

{
  "id" : 1,
  "first_name" : "Ramesh",
  "last_name" : "Fadatare",
  "full_name" : "Ramesh Fadatare"
}

通过使用@JsonProperty注解,您可以轻松地控制序列化过程中生成的JSON属性名称,从而满足特定的需求或符合外部API的要求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值