Java中jackson常用注解

在日常使用中,由于涉及到各种序列化和反序列化的处理,就不能不提 注解,了解注解的常用方式可以极大地方便我们处理序列化,下面介绍一些在使用Jackson中涉及到的注解。

1. @JsonProperty - 字段命名

@JsonProperty 注解用于在序列化时按照给定的字段名命名,在反序列化时,在 json 串中的注解字段给该字段设置属性值。

下面是注解的简单示例:

package org.example;
 
import com.fasterxml.jackson.annotation.JsonProperty;
 
public class PersonProperty {
    @JsonProperty("first_name")
    private String firstName;
 
    public PersonProperty() {
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}
public static void jsonPropertyDemo() {
    ObjectMapper objectMapper = new ObjectMapper();
    PersonProperty pp = new PersonProperty();
    pp.setFirstName("Alice");
 
    String jsonString = null;
    try {
        jsonString = objectMapper.writeValueAsString(pp);
        System.out.println("json property: " + jsonString);
    } catch (Exception e) {
        e.printStackTrace();
    }
 
    try {
        PersonProperty pp1 = objectMapper.readValue(jsonString, PersonProperty.class);
        System.out.println(pp1.getFirstName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2. @JsonPropertyOrder - 字段序列化顺序

@JsonPropertyOrder加在类上,用以规定数据序列化时字段出现的顺序。

package org.example;
 
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
 
// {"name":"Bob","id":"111","age":25,"phone":"12345678910"}
@JsonPropertyOrder({"name", "id", "age", "phone"}) 
// 没有定义顺序,就按照字典序排列,{"age":25,"id":"111","name":"Bob","phone":"12345678910"}
// @JsonPropertyOrder(alphabetic = true) 
public class PersonPropertyOrder {
    private String id;
 
    private String name;
 
    private int age;
 
    private String phone;
 
    public PersonPropertyOrder() {
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
}
public static void jsonPropertyOrder() {
    ObjectMapper objectMapper = new ObjectMapper();
    PersonPropertyOrder ppo = new PersonPropertyOrder();
    ppo.setAge(25);
    ppo.setId("111");
    ppo.setName("Bob");
    ppo.setPhone("12345678910");
 
    String jsonString = null;
    try {
        jsonString = objectMapper.writeValueAsString(ppo);
        System.out.println("json property: " + jsonString);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3. @JsonAlias - 字段别名,反序列化

在数据反序列化时,通过 @JsonAlias 注解来设置字段的值,只要是 alias中的和字段本身都可以正常反序列化。

package org.example;
 
import com.fasterxml.jackson.annotation.JsonAlias;
 
public class PersonAlias {
    @JsonAlias({"firstName", "personName"})
    private String name;
 
    public PersonAlias() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
public static void jsonAlias() {
    String jsonString1 = "{"name":"Bob"}";
    String jsonString2 = "{"firstName":"Bob"}";
    String jsonString3 = "{"personName":"Bob"}";
 
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        PersonAlias p1 = objectMapper.readValue(jsonString1, PersonAlias.class);
        PersonAlias p2 = objectMapper.readValue(jsonString2, PersonAlias.class);
        PersonAlias p3 = objectMapper.readValue(jsonString3, PersonAlias.class);
 
        System.out.printf("p1: %s, p2: %s, p3: %s", p1.getName(),p2.getName(), p3.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4. @JsonIgnore -序列化时忽略字段

@JsonIgnore 加在字段上,用以在序列化时,忽略其,在反序列化时,仅赋值null

package org.example;
 
import com.fasterxml.jackson.annotation.JsonIgnore;
 
 
public class PersonIgnore {
    private String 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.D.Chuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值