当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
MySQL01
什么是Inductive learning和Transductive learning
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
从mask-rcnn到shp
HCIP BGP建邻实验
Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
CLUSTER DAY04 (Block Storage Use Cases, Distributed File Systems, Object Storage)
uboot sets the default bootdelay
每日sql - 判断+聚合
arcgis填坑_4
iptables nat
抖音关键词搜索商品-API工具
HCIA experiment
每日sql -用户两天留存率
会议OA项目之我的会议
《Show and Tell: A Neural Image Caption Generator》论文解读
获取拼多多商品信息操作详情
Conference OA Project My Conference
知识蒸馏Knownledge Distillation
maxwell 概念



![[损失函数]——均方差](/img/55/e3c1797d5e87ddfb72bf39c5d31168.png)




