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
- When idea automatically generates getter and setter, it automatically ignores is
- When fastjson is serialized, traverse the getter, remove the get, and use the latter as the attribute name.
- 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
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
3. Change the serialization method (not recommended)
There are two ways to serialize
- jdk native
- json
- fastjson
- jackson
- 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;
}
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
References
- Solve the problem that IDEA automatically generates get and set methods when the variable name of boolean type starts with is - copy and paste a shuttle - 博客园 (cnblogs.com)
- The reason, solution and thinking of missing is in front of the property name when java serializes boolean
- Boolean variables in the POJO class should not be prefixed with is - flunggg - 博客园 (cnblogs.com)
- Why does the Alibaba Development Manual mandate that variables of Boolean type in POJO classes should not be prefixed with is?_o Murong Ziying o's Blog-CSDN Blog