Spring Framework 7.031.空安全性

Spring Framework 7.031.空安全性

Spring Framework 从 5.0 版本开始引入了对空安全性(null safety)的支持,通过使用 JSR 305 注解(如 @Nullable@NonNull)来明确标识参数、返回值和字段的空值可能性。这有助于开发者在编译时检测潜在的空指针异常(NullPointerException),并提高代码的可靠性。

空安全注解

Spring 主要使用以下注解来标记空值相关的语义(这些注解来自 org.springframework.lang 包):

  • @Nullable:标识一个元素(参数、返回值、字段)可以为 null
  • @NonNull:标识一个元素不可以null(默认情况下,未标记的元素通常被视为非空)。
  • @NonNullApi:包级别的注解,声明该包下的所有 API 元素(参数、返回值)默认都是非空的,除非显式标记 @Nullable
  • @NonNullFields:包级别的注解,声明该包
public static void buildInterpretResByName(List<InterpretRes> interpretResList, List<JSONObject> resultKnowledgeArray) throws Exception{ if(!(interpretResList.size() == resultKnowledgeArray.size())){ throw new Exception("Target count does not match result count"); } for (int i = 0; i < resultKnowledgeArray.size(); i++) { InterpretRes interpretRes = interpretResList.get(i); JSONObject json = resultKnowledgeArray.get(i); // 设置阿拉伯语字段 interpretRes.setTypename_ar(json.getString("level1_ar")); interpretRes.setClassname_ar(json.getString("level4_ar")); interpretRes.setModelname_ar(json.getString("level5_ar")); // 设置中文字段 interpretRes.setTypename_ch(json.getString("level1")); interpretRes.setClassname_ch(json.getString("level4")); interpretRes.setModelname_ch(json.getString("level5")); // 设置英语字段(默认) interpretRes.setTypename(json.getString("level1_en")); interpretRes.setClassname(json.getString("level4_en")); interpretRes.setModelname(json.getString("level5_en")); interpretRes.setLevelId(json.getInteger("level_id")); interpretRes.setResType(json.getString("res_type")); } }在JSONObject json = resultKnowledgeArray.get(i); 报15:57:54.031 [http-nio-9204-exec-7] ERROR c.a.i.s.i.ParseInterpretResServiceImpl - [parseTargetResult,185] - xml parse error:{} java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.alibaba.fastjson.JSONObject at com.aerors.interpretation.utils.BuildTargetUtils.buildInterpretResByName(BuildTargetUtils.java:197) at com.aerors.interpretation.service.impl.ParseInterpretResServiceImpl.parseTargetResult(ParseInterpretResServiceImpl.java:176) at com.aerors.interpretation.service.impl.ParseInterpretResServiceImpl.parseTargets(ParseInterpretResServiceImpl.java:83) at com.aerors.interpretation.service.impl.ParseInterpretationMissionServiceImpl.insertInterpretationMission(ParseInterpretationMissionServiceImpl.java:86) at com.aerors.interpretation.controller.ParseInterpretationMissionController.interpretationMissionArchive(ParseInterpretationMissionController.java:37) at com.aerors.interpretation.controller.ParseInterpretationMissionController$$FastClassBySpringCGLIB$$8cbb5898.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:792) 如何解决
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员勇哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值