Java——jackson的注解@JsonProperty、@JsonIgnore、@JsonFormat、@JsonIgnoreProperties

这篇博客介绍了如何使用Jackson库将Java对象转换为JSON字符串,并展示了反序列化过程。通过`@JsonProperty`、`@JsonIgnore`、`@JsonFormat`和`@JsonIgnoreProperties`等注解,控制序列化和反序列化的细节。测试结果显示,注解成功地影响了字段的序列化和反序列化行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用

1.pom

<dependency> 
  <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
  <version>2.11.0</version>
</dependency>

2.实体类 Student


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@JsonIgnoreProperties(value = { "address", "score" })
public class Student implements Serializable {

    @JsonProperty(value = "sName", required = true)
    private String name;

    @JsonIgnore
    private int age;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
    private Date birthday;

    private String address;

    private String score;

}

3.测试类

public class TestMain {

    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setName("张三");
        student.setAge(20);
        student.setBirthday(new Date());
        student.setAddress("上海市");
        student.setScore("90");

        // 使用writeValuesAsString把对象转化成json字符串。
        String stuJson = new ObjectMapper().writeValueAsString(student);
        System.out.println(stuJson);

        //使用readValue把字符串转化为对象
        Student stu = new ObjectMapper().readValue(stuJson, Student.class);
        System.out.println(stu.toString());

        System.out.println("==============================");

        String json1 = "{\"age\":20,\"birthday\":\"2022-04-18 09:38:47\",\"address\":\"上海市\",\"score\":\"90\",\"sName\":\"张三\"}";

        Student stu1 = new ObjectMapper().readValue(json1, Student.class);
        System.out.println(stu1.toString());
    }

}

测试结果:

{"birthday":"2022-04-18 09:42:58","sName":"张三"}
Student(name=张三, age=0, birthday=Mon Apr 18 17:42:58 CST 2022, address=null, score=null)
==============================
Student(name=张三, age=0, birthday=Mon Apr 18 17:38:47 CST 2022, address=null, score=null)

2.说明

@JsonProperty

value属性: 代表该属性序列化和反序列化的时候的key值。
required属性: 默认false,例如当required=true的时候,当反序列化的时候没有找到key值,就会报错。

@JsonIgnore

json序列化 会忽略这个字段
json反序列化,会忽略这个字段的赋值

@JsonFormat

json序列化,通过后面设置的格式进行数据的输出
json反序列化,通过设定的格式 进行数据的赋值

@JsonIgnoreProperties

类注解,
json序列化时,会忽略这些字段
json反序列化,会忽略这些字段的赋值

### 使用@JsonProperty注解处理JSON字段映射 在Java中,`@JsonProperty` 注解用于 Jackson 库来定义 JSON 字符串中的属性名与 Java 对象中的属性名之间的映射关系[^1]。 当序列化或反序列化对象时,如果希望 JSON 中的键名不同于类成员变量的名字,则可以在成员变量、getter 或 setter 方法上使用 `@JsonProperty` 来指定对应的 JSON 键名[^3]。 下面是一个简单的例子展示如何利用此注解: ```java import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; public class User { private String id; @JsonProperty("full_name") // 将 JSON 的 full_name 映射到 name 属性 private String name; public User() {} public static void main(String[] args) throws Exception{ ObjectMapper mapper = new ObjectMapper(); // 创建一个User实例并转换成JSON字符串 User user = new User(); user.setId("007"); user.setName("James Bond"); System.out.println(mapper.writeValueAsString(user)); // 输出 {"id":"007","full_name":"James Bond"} } // Getters and Setters with JsonProperty annotations @JsonProperty("user_id") public String getId() {return this.id;} @JsonProperty("user_id") public void setId(String value){this.id=value;} @JsonProperty("full_name") public String getName(){return this.name;} @JsonProperty("full_name") public void setName(String fullNameValue){ this.name=fullNameValue; } } ``` 在这个案例里,通过设置 `@JsonProperty` 到 getter 和 setter 上面,实现了即使类内部使用的属性名为 "name", 但在生成的 JSON 数据中会显示为 "full_name"。同样地,“id”的情况也是如此,在 JSON 表达式中它被表示成了"user_id".
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值