Detailed explanation of annotations and usage in Jackson Core frame
Detailed explanation of annotations and usage in Jackson Core frame
Jackson is a popular Java library that is used to serialize the Java object to JSON and the backflow of it back to the Java object.The library is widely used in fields, such as the RESTFUL Web service, distributed system and big data processing.The Jackson Core framework provides a set of powerful annotations to control the behavior in the process of JSON serialization and derivativeization.This article will explore the annotations and usage in the Jackson Core framework.
1. @JSONINCLUDE Note
@JSONINCLUDE annotations are used to specify the attributes that should be included when serializing the Java object to JSON.This annotation has the following options:
-@JSONINCLUDE (jsoninclude.include.alway): Always include attributes, even if its value is NULL or default value.
-@JSONINCLUDE (jsoninclude.include.non_null): It contains attributes when the attribute value is not NULL.
-@JSONINCLUDE (JSONINCLUDE.INCLUDE.NON_EMPTY): only contains attributes when the attribute value is not NULL and does not be empty string or when set.
Example:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {
private String name;
private Integer age;
private String address;
// getters and setters
}
2. @jsonproperty annotation
@Jsonproperty annotation is used to specify the name of the attribute in JSON.Can be used to establish mapping between the attribute name of the Java object and the JSON key.By default, the name of the attribute is the same as the JSON key.
Example:
public class Person {
@JsonProperty("full_name")
private String name;
private Integer age;
private String address;
// getters and setters
}
3. @jsonSetter and @jsongetter annotation
@JsonSetter and @jsongetter annotations are the names of Setter and Getter methods used to specify the attributes in the process of JSON serialization and back -sequencing.
Example:
public class Person {
private String name;
private Integer age;
private String address;
@JsonSetter("full_name")
public void setName(String name) {
this.name = name;
}
@JsonGetter("full_name")
public String getName() {
return name;
}
// other getter and setter methods
}
4. @jsonignore annotation
@JSONIGNORE annotations should be ignored during the process of specifying a certain attribute during serialization and desertification.
Example:
public class Person {
private String name;
private Integer age;
@JsonIgnore
private String address;
// getters and setters
}
5. @JSONFORMAT Note
@JSONFORMAT annotations are used to specify the format of the attribute during serialization and derivativeization.Can be used to specify the format of date, time, number, etc.
Example:
public class Person {
private String name;
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private BigDecimal balance;
// getters and setters
}
6. @JSONMANAGEDREFERENCE and @JSONBACKREFERENCE
@JSONMANAGEDREFERENCE and @JSONBACKREFERENCE annotations are used to solve the problem of cycle reference.When the two objects are quoted with each other, these annotations can specify which one is the main reference object and which is a passive reference object.
Example:
public class Order {
private int orderId;
@JsonManagedReference
private Customer customer;
// other properties, getters and setters
}
public class Customer {
private int customerId;
@JsonBackReference
private List<Order> orders;
// other properties, getters and setters
}
The above is an introduction to some commonly used annotations in the Jackson Core framework.These annotations can help developers more accurately control the behavior of JSON serialization and derivativeization, making it more in line with actual needs.By flexibly using these annotations, you can easily handle complex object relationships, ignore the serialization format of specific attributes, and adjust attributes.