当前位置:网站首页>Do not add the is prefix to the variables of the boolean type in the POJO class of the Alibaba specification
Do not add the is prefix to the variables of the boolean type in the POJO class of the Alibaba specification
2022-08-11 07:13:00 【geekmice】
说明
【强制】POJO 类中的任何布尔类型的变量,都不要加 is 前缀,否则部分框架解析会引起序列
化错误.
说明:在本文 MySQL 规约中的建表约定第一条,表达是与否的变量采用 is_xxx 的命名方式,所以,需要
in settings from is_xxx 到 xxx 的映射关系.
反例:定义为基本数据类型 boolean isDeleted 的属性,它的方法也是 isDeleted(),框架在反向解析的时
候,“误以为”对应的属性名称是 deleted,导致属性获取不到,进而抛出异常.
问题复现
准备工作
1、需要的依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
2、对应java文件
public class App {
public static void main(String[] args) {
TempOne temp = new TempOne();
temp.setLive(true);
TempTwo tempTwo = new TempTwo();
tempTwo.setLive(true);
System.out.println("temptwo isLive:" + new Gson().fromJson(JSON.toJSONString(tempTwo),TempTwo.class));
System.out.println("temp live:"+ new Gson().fromJson(JSON.toJSONString(temp), TempOne.class));
}
}
3、执行后效果
temptwo isLive:TempTwo{isLive=false}
tempone live:Temp{live=true}
4、结果分析
TempTwoclass has beenisLive属性设置为true,Why return is indeedfalse,原因是因为JSON框架通过扫描所有的getter后发现有一个isLive方法,然后根据JavaBeans的规范,解析出变量名为live,把model对象序列化城字符串后内容为{“live”: true}.
然后根据{“live”: true}这个json串,Gson框架在通过解析后,通过反射寻找People类中的live属性,但是TempTwo类中只有isLive属性,找不到对应的属性,所以,最终反序列化后的TempTwo类的对象中,isLive则会使用默认值false
As a result, the boolean type data in the foreground cannot be sent to the background,Since the front desk will pass it over, it will be namedisXThe variable resolves to X,但是我们pojoThe Boolean type property in the class isisX,导致找不到X,So passing the value fails;
Finally explained:pojoDo not add boolean-like variablesis前缀
注意点
所有的POJO类属性必须使用包装数据类型.
RPC方法的返回值和参数必须使用包装数据类型.
所有的局部变量使用基本数据类型.
boolean类型的默认值为false;而Boolean类型的默认值是null
边栏推荐
猜你喜欢
随机推荐
什么是Inductive learning和Transductive learning
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
MySQL之CRUD
Especially the redis
阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
OA项目之会议通知(查询&是否参会&反馈详情)
损失函数——交叉熵
Eight-legged text of mysql
【LeetCode】2034. 股票价格波动(思路+题解)双map
FusionCompute8.0.0实验(1)CNA及VRM安装
deepin v20.6+cuda+cudnn+anaconda(miniconda)
矩阵分析——矩阵分解
WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
淘宝商品详情API接口
HCIP WPN experiment
LabelEncoder和LabelBinarizer的区别
会议OA项目之我的会议
The ramdisk practice 1: the root file system integrated into the kernel
《Generative Adversarial Networks》
uboot sets the default bootdelay