Serialization and deserialization in Java (4): The problem of capitalizing the first letter or first two letters of Java class properties to get the property value to be null

The problem that the first letter or the first two letters of the Java class attribute is capitalized to get the attribute value to be null

1 Overview

Hello everyone, I am Ouyang Fangchao.
The Spring Boot project defines a DTO with uppercase attributes. When using it as the receiving object of the interface, you will find that the received object ID attribute is always null.

import lombok.Data;

@Data
public class Fa {
    
    
    private String ID;
}

2. Solution

2.1. Solution

This problem occurs because of Jackson deserialization. The specific reason will not be discussed in detail this time. There are two solutions.
Method 1
Method 1 is to add the @JsonProperty annotation to the corresponding field.

@JsonProperty(value = "ID")
private String ID;

Method 2
Method 2 is to name the attribute in a standardized manner. Specifically, the first two letters of the attribute name cannot be capitalized, as described below:

Don’t capitalize first two letters of a bean property name

2.2. Deepening the problem

The uppercase naming method above, if you use com.alibaba.fastjson.JSON.toJSONString(param) to convert it into a json string, problems will also occur. The specific problem is this:
if you only add @JsonProperty(value = " ID"), the attribute named iD will be obtained in the json string (note that the letter i is lowercase);
if only @JSONField(name = "ID") is added, the ID attribute will not be obtained in the json string;
if ID Add both @JsonProperty(value = "ID") and @JSONField(name = "ID") to the attribute, then the ID attribute will be normally obtained in the converted json string; in order to avoid such problems, it is still
from Let's start with the naming convention of Java properties.

3. Other things to say

Now that we have talked about the @JsonPropertyOrder annotation, let’s introduce @JsonPropertyOrder again, which acts on the class and is used to specify the order of the properties in the serialized JSON object. for example:

@JsonPropertyOrder({
    
     "name", "age", "address" })
class MyObject {
    
    
    private String address;
    private int age;
    private String name;
    
    // getters and setters
}

When serializing to JSON, Jackson will arrange the properties in the JSON object in the order specified in the @JsonPropertyOrder annotation, that is, first output name, then age, and finally address. It should be noted that if the order of properties is not specified in the @JsonPropertyOrder annotation, Jackson will arrange the properties in the JSON object in the order defined by the properties. If some properties are not declared in the @JsonPropertyOrder annotation, then these properties will be arranged after the declared properties in the default order.
You can also sort properties alphabetically:

@JsonPropertyOrder(alphabetic = true)
class MyObject {
    
    
    private String address;
    private int age;
    private String name;
    
    // getters and setters
}

4. Summary

A certain attribute of the object received in the interface is empty. An accidental interface troubleshooting made me realize this problem. In short, it is more scientific to start from the naming convention of Java attributes. The last thing to mention is that you can use the @JsonPropertyOrder annotation to sort properties.
I'm Ouyang Fangchao, and I'm interested in doing things well. If you like my article, please like, forward, comment and pay attention. See you next time.

Guess you like

Origin blog.csdn.net/u012288582/article/details/131310117