Java entity class attributes and database fields are capital letters, why return to the front end will become lowercase letters

Java entity class attributes and database fields are capital letters, why return to the front end will become lowercase letters

1. Problem description

Recently, I found a problem in the process of developing the system. The field names of the data tables in my database are all capital letters, and the attributes in the Java entity class are also capital letters, but when they are returned to the front end, all the attribute names change. became lowercase.
I solved the problem after consulting the information, so I recorded it to avoid stepping on the pit again in the future.

  • The field names of the database fields are capital letters, as follows
ID NAME
1 little red
2 Xiao Ming
  • Java entity class attributes are also capital letters, as follows
public class Person {
    
    
    private Integer ID;
    private String NAME;
    
    // Getter and Setter
}
  • The front end receives lowercase letters, as follows
{
    
    
    "data": [
        {
    
    
            "id": 1,
            "name": "小红"
        },
        {
    
    
            "id": 2,
            "name": "小明"
        }
    ]
}

2. Reason

After consulting the information, I found that this is because the naming convention in Java is to use camel case naming , that is, the first word of the attribute name is lowercase, and the first letter of the subsequent word is capitalized.

However, when returning to the front end, the JSON format is usually used, and the attribute names in JSON are case-sensitive, so the attribute names will be converted to lowercase letters.

If you want to preserve the case of the property name when returning it to the front end, you can use the @JsonProperty annotation in the Jackson library to specify the property name.

3. Solutions

Modify the entity class as follows


import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
    
    

    @JsonProperty("ID")
    private Integer ID;

    @JsonProperty("NAME")
    private String NAME;

    // Getter and Setter
}

Guess you like

Origin blog.csdn.net/qq_45256357/article/details/130489338