Different usage scenarios of @JSONField and @JsonProperty

Preface

It is known that both the @JSONField and @JsonProperty annotations are added to the properties of the bean and are used to perform special processing on the corresponding fields when the object is converted into a Json object.
However, the two seem to have the same function, so confusion sometimes occurs when using them. This article uses several common usage scenarios as examples to try to explain the differences between them.

@JSONField

Belonging component

The @JSONField annotation comes from the following packages: com.alibaba.fastjson.annotation.JSONField.
It can be seen that it belongs to the class in the fastjson component, and fastjson is a common JSON library and is used frequently.

scenes to be used

Well, since it is an annotation in the fastjson library, it will only take effect when using components in fastjson to convert beans to JSON strings, such as:

// JSON是fastjson包中的
JSON.toJSON(event)

@JsonProperty

Belonging component

The @JSONField annotation comes from the following packages: com.fasterxml.jackson.annotation.JsonProperty.
It can be seen that it belongs to the class in the Jackson component, but it seems that the Jackson component is not actively referenced in the project?
View the reference tree of pom dependencies:
spring-boot-starter-web refers to jackson
You can see common spring-boot-starter-webdependencies in Java development, the bottom layer of which references jackson-corethis package, and the @JsonProperty annotation is in this package.

scenes to be used

Therefore, we can say that spring boot officially uses the jackson component to operate Json objects.
Here are two common areas of influence:

  1. @RequestBody annotation commonly used in control layer method input parameters
  2. The control layer directly returns the entity object

@RequestBody

When the front end uses the POST method to transmit a Json string to the back end, we usually use the @RequestBody annotation to modify the input parameter object of the corresponding control layer method so that the content in the Json string can be mapped to an entity class, and this process is Jackson’s @JsonProperty annotation is used.

The control layer directly returns the entity object

When the control layer directly returns the entity object, it does not call the object's toString method, but automatically calls the jackson component to complete the Json serialization of the object.

Summarize

@JSONField and @JsonProperty belong to two different packages. The former is the Alibaba fastjson package, and the latter is the jackson package officially used by spring boot.
Generally, when using the spring framework to automatically generate or parse Json-related data, the jackson package will be called, and @JsonProperty will take effect; when the fastjson component is manually introduced and the corresponding Json object (de)serialization is performed, @JSONField will take effect.

Guess you like

Origin blog.csdn.net/Ka__ze/article/details/132494233