[Java] Do not name the attribute of the POJO class as isXXX Problem description, reproduction, solution

Problem Description

When the attribute of POJO is Boolean type, the naming method of is_xxx is adopted, and the parameter cannot be found.

Problem recurrence

The property of Boolean is named isA, but its getter and setter automatically cancel is

public class Person {
    
      
    Boolean isA;  
  
    public Boolean getA() {
    
      
        return isA;  
    }  
  
    public void setA(Boolean a) {
    
      
        isA = a;  
    }  
}

The front-end and back-end transfer objects are serialized through fastjson

Person person = new Person();  
person.setA(true);  
System.out.println(person);  
  
// 将Java对象转换为json字符串  
String jsonString= JSON.toJSONString(person);  
System.out.println(jsonString);
Person{isA:true}
{"a":true}

It can be seen that the isA attribute of the serialized object becomes a, so the front end may make an error

The front-end passed the json, but the back-end parsed it incorrectly

String jsonString = "{\"isA\":true}";  
// 将json反序列化为Java对象  
Person person1 = JSON.parseObject(jsonString,Person.class);  
System.out.println(person1);
Person{isA:null}

reason

  1. When idea automatically generates getter and setter, it automatically ignores is
  2. When fastjson is serialized, traverse the getter, remove the get, and use the latter as the attribute name.
  3. When fastjson is deserialized, add set to the attribute name and traverse the setter

Solution

1. Correct getters and setters (not recommended)

1.1 Manual correction is too troublesome

public Boolean getIsA() {
    
      
    return isA;  
}  
  
public void setIsA(Boolean a) {
    
      
    isA = a;  
}

1.2 Correct the code template of the idea, but it does not conform to the habits of the public

1.3 Use lombok, not all projects will use it
image.png

2. Correct the attribute name isXXX to XXX (recommended)

Follow this encoding and naming convention, then all problems will be solved, the simplest,
which is also suggested by the Ali manual
image.png

3. Change the serialization method (not recommended)

There are two ways to serialize

  1. jdk native
  2. json
    1. fastjson
    2. jackson
    3. Both gson
      fastjson and jackson assign values ​​to attributes through getters and setters, but gson assigns values ​​directly through attributes, so the above problems will not occur

However, there are still many domestic projects using fastjson, and jackson is the default of springboot

expand

Generally do not use basic data types for attribute naming.
The situation of using boolean is slightly different. Whether it is automatically generated by idea or the get method of lombok, it becomes isA()

boolean isA;  
  
public boolean isA() {
    
      
    return isA;  
}  
  
public void setA(boolean a) {
    
      
    isA = a;  
}

image.png

But the problem is the same as above

The default value is not null, but false, which is logically unreasonable in some cases, so it is not recommended to use basic data types as attributes, which is also mandatory in the Ali manual
image.pngimage.png

References

Guess you like

Origin blog.csdn.net/weixin_50799082/article/details/131307902
Recommended