Detailed explanation of @JsonProperty, @JsonFormat and @DateTimeFormat annotation usage

1. @JsonProperty annotation

@JsonPropertyAnnotation is an annotation provided by the Jackson library, which is used to establish a mapping relationship between the properties of Java objects and JSON fields. Through this annotation, you can customize the name of the JSON field corresponding to the attribute during serialization and deserialization.

Usage

Add @JsonProperty annotation on the attribute that needs to be mapped, and specify the name of the JSON field as a parameter. For example:

public class Person {
    
    
    @JsonProperty("name")
    private String fullName;

    // Getter and setter methods
}

In the above example, the fullName property will be mapped to the "name" field during JSON serialization and deserialization.

2. @JsonFormat annotation

@JsonFormatAnnotations are used to specify the format of properties during serialization and deserialization. It provides options for formatting specific types of properties such as dates, times, and numbers.

Usage

When using the @JsonFormat annotation, you can specify one or more of the following parameters:

  • pattern: Define the format mode of date, time or number.
  • shape: Specifies the serialization and deserialization method of the attribute. Can be JsonFormat.Shape.STRING, JsonFormat.Shape.NUMBER, JsonFormat.Shape.OBJECT, JsonFormat.Shape.ARRAY or JsonFormat.Shape.BOOLEAN.
  • locale:Specify the locale settings to use.
  • timezone:Specify time zone.

For example, use the @JsonFormat annotation to format a date:

public class Event {
    
    
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date eventDate;

    // Getter and setter methods
}

In the above example,eventDate the properties will be serialized and deserialized in the specified format "yyyy-MM-dd".

3. @DateTimeFormat annotation

@DateTimeFormatThe annotation is an annotation provided by the Spring framework for parsing and formatting properties of date and time types. It is usually used with the @RequestMapping or @ModelAttribute annotation.

Usage

Add @DateTimeFormat annotation on the attributes that need to be parsed and formatted, and specify the date and time format as parameters. For example:

public class Event {
    
    
    private String name;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date eventDate;

    // Getter and setter methods
}

In the above example,eventDate the attribute will be parsed and formatted using the specified format "yyyy-MM-dd".

This annotation is mainly used to process the date or time data submitted by the form and convert it into the properties of the Java object. At the same time, it can also format the date and time properties of the Java object into a specific string form to be returned in the response.

Summarize

  • @JsonPropertyAnnotations are used to specify the mapping between Java object properties and JSON fields.
  • @JsonFormatAnnotations are used to define the serialization and deserialization format of properties.
  • @DateTimeFormatAnnotations are used to parse and format properties of datetime type, usually used together with the request mapping annotations in the Spring framework.

All three annotations provide a way to customize the behavior of properties during serialization and deserialization, allowing developers to handle conversions between objects and JSON more flexibly.

It should be noted that the @JsonProperty annotation is provided by the Jackson library, while the @JsonFormat and @DateTimeFormat annotations are provided by the Spring framework which provided. They are used in different contexts, but both facilitate more precise conversion of objects to and from JSON.

These annotations can play a role in various scenarios. For example, customizing JSON field names, date and time formats, number formats, etc. are very useful when using RESTful APIs for data transmission. They give developers greater control over the format of their data, increasing interactivity and flexibility.

In general, the @JsonProperty, @JsonFormat, and @DateTimeFormat annotations provide developers with a smooth interface between objects and JSON. The flexibility in mapping and formatting makes the data serialization and deserialization process more customizable and precise. These annotations play an important role in actual development, helping developers handle data in different formats and ensuring the correct transmission and parsing of data.

おすすめ

転載: blog.csdn.net/xiangyuWA/article/details/130995946