TypechoJoeTheme

阿尔法色色之屋

【Json】Jackson 常用注解 详解

本文最后更新于2023年10月19日。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
为了有更好的阅读体验,阿尔法色色之屋为长篇文章提供了目录树,若没有正常显示,则需要手动缩小页面比例。
本文所写注解位于com.fasterxml.jackson.annotation包中

相关依赖

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

基本使用

java代码中常常用到jackson的注解,主要用到的有:@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat

举例:

实体类Student

@JsonIgnoreProperties(value = { "address", "score" })
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @JsonIgnore
    private int age;

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

    private String address;

    private String score;

    public String getTrueName() {
        return trueName;
    }

    public void setTrueName(String trueName) {
        this.trueName = trueName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "name: " + this.trueName + ", address: " + this.address + ", age:" 
                 + this.age + ", birthday:" + this.birthday + ", score:" + this.score;
    }
}

测试注解:

public class TestJsonProperty {
    public static void main(String[] args) throws IOException {
        Student student = new Student();
        student.setTrueName("张三");
        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());
    }
}

结果:

{"birthday":"2020-04-16 03:57:06","name":"张三"}
name: 张三, address: null, age:, birthday:Thu Apr 16 11:57:06 CST 20200, score:null

常用注解

@JsonProperty

写法有两种:

第1种:全写,使用“=”;
@JsonProperty(value = "", required = true)

第2种:简写,直接把value写到括号里,required默认为false。
@JsonProperty("")

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

@JsonIgnore

一般标记在属性或者方法上,返回的json数据即不包含该属性

@JsonIgnoreProperties

类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

@JsonFormat

用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式

写法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")

@JSONField

主要用于解析前端传过来的时间格式的数据
用法:@JSONField(format = "yyyy-MM-dd")

赞(0)