Jackson common annotations and application scenarios

basic introduction

Jackson is a Java library for serializing Java objects to JSON format or deserializing JSON format to Java objects. Jackson provides a series of annotations to control the behavior during serialization and deserialization. The main features of Jackson are as follows:
① Fast: Jackson is a high-performance JSON processing library that can quickly serialize Java objects into JSON format or deserialize JSON format into Java objects.
② Flexible: Jackson provides a wealth of annotations, which can be used to control the behavior during serialization and deserialization, such as specifying JSON attribute names, date formats, ignoring a certain attribute, etc.
③ Ease of use: Jackson's API is simple and easy to use, which can easily serialize Java objects into JSON format or deserialize JSON format into Java objects.
Support multiple data formats: In addition to JSON format, Jackson also supports multiple data formats such as XML and YAML.
④ Open source: Jackson is an open source Java library that can be used and modified for free.


Common Notes


@JsonProperty annotation

Application scenario:

In actual development, we may need to serialize Java objects into JSON format, or deserialize JSON format into Java objects. In this process, we may need to control the names of JSON attributes to make them more suitable for our needs. At this point, you can use the @JsonProperty annotation to specify the property name.

for example:

Suppose there is a Java class Person, which has two attributes: name and age. We hope that when the class is serialized into JSON format, the JSON attribute name corresponding to the attribute name is "full_name", and the JSON attribute name corresponding to the attribute age is "years_old". At this point, you can use the @JsonProperty annotation to specify the property name.
The sample code is as follows:

public class Person {
    @JsonProperty("full_name")
    private String name;
    @JsonProperty("years_old")
    private int age;
    // getters and setters
}

For example, if there is a Person object whose name attribute is "Zhang San" and whose age attribute is 25, then the serialized JSON format is as follows:

{
    "full_name": "张三",
    "years_old": 25
}

@JsonFormat annotation

Application scenario:

In actual development, we may need to serialize Java objects into JSON format, or deserialize JSON format into Java objects. In this process, we may need to control the date format to make it more suitable for our needs. At this time, you can use the @JsonFormat annotation to specify the date format.

for example:

Suppose there is a Java class Event, which has two properties: name and date. We hope that when the class is serialized into JSON format, the date format corresponding to the attribute date is "yyyy-MM-dd HH:mm:ss". At this time, you can use the @JsonFormat annotation to specify the date format.
The sample code is as follows:

public class Event {
    private String name;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date date;
    // getters and setters
}

In the above code, we use the @JsonFormat annotation to specify the date format. When serializing the Event object into JSON format, the attribute date will be formatted as a string of "yyyy-MM-dd HH:mm:ss".
For example, if there is an Event object whose name attribute is "meeting" and whose date attribute is 10:30 on January 1, 2022, the serialized JSON format is as follows:

{
    "name": "会议",
    "date": "2022-01-01 10:30:00"
}

@JsonIgnore annotation

Application scenario:

In actual development, we may need to serialize Java objects into JSON format, or deserialize JSON format into Java objects. In this process, we may need to ignore certain properties so that they do not participate in serialization and deserialization. At this time, you can use the @JsonIgnore annotation to specify to ignore attributes.

for example:

Suppose there is a Java class Person, which has two attributes: name and age. We want the property age to be ignored when serializing this class to JSON format. At this time, you can use the @JsonIgnore annotation to specify to ignore attributes.
The sample code is as follows:

public class Person {
    private String name;
    @JsonIgnore
    private int age;
    // getters and setters
}

In the above code, we use the @JsonIgnore annotation to specify ignore properties. When serializing a Person object to JSON format, the attribute age will be ignored.
For example, if there is a Person object whose name attribute is "Zhang San" and whose age attribute is 25, then the serialized JSON format is as follows:

{
    "name": "张三"
}

@JsonUnWrapped annotation

Application scenario:

In actual development, we may need to serialize Java objects into JSON format, or deserialize JSON format into Java objects. In this process, we may need to control the expansion of attributes to make it more in line with our needs. At this time, you can use the @JsonUnwrapped annotation to specify attribute expansion.

for example:

Suppose there is a Java class Person, which has two attributes: name and address. Among them, address is a nested Java object, which has two properties: province and city. We want the address property to be expanded into the Person object when the class is serialized into JSON format. At this time, you can use the @JsonUnwrapped annotation to specify attribute expansion.
The sample code is as follows:

public class Person {
    private String name;
    @JsonUnwrapped
    private Address address;
    // getters and setters
}

public class Address {
    private String province;
    private String city;
    // getters and setters
}

In the above code, we use the @JsonUnwrapped annotation to specify property unwrapping. When serializing a Person object into JSON format, the property address will be expanded into the Person object.
For example, if there is a Person object whose name attribute is "Zhang San", the province of the address attribute is "Guangdong Province", and the city attribute is "Shenzhen", then the serialized JSON format is as follows:

{
    "name": "张三",
    "province": "广东省",
    "city": "深圳市"
}

@JsonView annotation

Application scenario:

In actual development, we may need to serialize Java objects into JSON format, but different scenarios need to display different properties. For example, a user object only needs to display the user name and avatar when displaying the list, but needs to display more attributes when displaying the details. At this time, you can use the @JsonView annotation to specify different views and control the display of attributes.

for example:

Suppose there is a Java class User, which has three attributes: id, name and avatar. We want to display different properties according to different views when serializing this class into JSON format. For example, a list view only displays the id and name attributes, and a detail view displays all attributes. At this point, you can use the @JsonView annotation to specify different views.
The sample code is as follows:

public class User {
    @JsonView(Views.List.class)
    private Long id;
    @JsonView({Views.List.class, Views.Detail.class})
    private String name;
    @JsonView(Views.Detail.class)
    private String avatar;
    // getters and setters
}

public class Views {
    public static class List {}
    public static class Detail extends List {}
}

In the above code, we use @JsonView annotation to specify different views. When serializing the User object into JSON format, different properties are displayed according to different views.
For example, if there is a User object whose id attribute is 1, name attribute is "Zhang San", and avatar attribute is "http://example.com/avatar.jpg", then the serialized JSON format is as follows:
list view:

{
    "id": 1,
    "name": "张三"
}

Details view:

{
    "id": 1,
    "name": "张三",
    "avatar": "http://example.com/avatar.jpg"
}

@JsonManagedReference and @JsonBackReference annotations

Application Scenario

In actual development, we may need to serialize Java objects into JSON format, but there are circular references between Java objects. For example, there is a one-to-many relationship between a user object and an order object, and an order object is associated with a user object. At this time, there will be a problem of circular reference, which will lead to the problem of infinite recursion when serializing to JSON format. At this time, you can use @JsonManagedReference and @JsonBackReference annotations to solve circular reference problems.

for example

Suppose there are two Java classes User and Order with a one-to-many relationship between them. A user can have multiple orders, and an order belongs to only one user. We want to avoid circular reference issues when serializing these two classes into JSON format. At this time, you can use @JsonManagedReference and @JsonBackReference annotations to solve circular reference problems.
The sample code is as follows:

public class User {
    private Long id;
    private String name;
    @JsonManagedReference
    private List<Order> orders;
    // getters and setters
}

public class Order {
    private Long id;
    private String name;
    @JsonBackReference
    private User user;
    // getters and setters
}

In the above code, we use @JsonManagedReference annotation and @JsonBackReference annotation to solve the circular reference problem. When serializing the User object into JSON format, the @JsonManagedReference annotation specifies the orders attribute as a managed attribute, and when serializing the Order object into JSON format, the @JsonBackReference annotation specifies the user attribute as a back-referenced attribute.
For example, if there is a User object whose id attribute is 1, name attribute is "Zhang San", and the orders attribute contains two Order objects, then the serialized JSON format is as follows:

{
    "id": 1,
    "name": "张三",
    "orders": [
        {
            "id": 1,
            "name": "订单1"
        },
        {
            "id": 2,
            "name": "订单2"
        }
    ]
}

おすすめ

転載: blog.csdn.net/m0_37742400/article/details/130520154