十大职业资格培训机构,昆山网站建设方案优化公司,wordpress图片专辑,广东三网合一网站建设报价Jackson 属性名大小写 Bug 记录
问题描述
在前后端交互过程中#xff0c;前端传递的 JSON 字段名为驼峰风格#xff08;如 qTitle#xff09;#xff0c;后端 Java 实体类字段名也为驼峰#xff08;如 private String qTitle;#xff09;。 但在反序列化时#xff0c;…Jackson 属性名大小写 Bug 记录
问题描述
在前后端交互过程中前端传递的 JSON 字段名为驼峰风格如 qTitle后端 Java 实体类字段名也为驼峰如 private String qTitle;。 但在反序列化时发现后端接收到的 qTitle 字段始终为 null导致如 NotBlank(message 问卷标题不能为空) 校验失败。
现象
前端请求体{qTitle: 2024年员工满意度调查问卷,...
}后端实体类public class QuestionnaireCreateRequest {NotBlank(message 问卷标题不能为空)private String qTitle;// ...
}实际后端接收到的 qTitle 字段为 null触发校验异常。
排查过程
确认前后端字段名一致均为驼峰。查看 Jackson 源码定位到 legacyManglePropertyName 方法 /*** Method called to figure out name of the property, given * corresponding suggested name based on a method or field name.** param basename Name of accessor/mutator method, not including prefix* (get/is/set)*/protected String legacyManglePropertyName(final String basename, final int offset){final int end basename.length();if (end offset) { // empty name, nopereturn null;}char c basename.charAt(offset);// 12-Oct-2020, tatu: Additional configurability; allow checking that// base name is acceptable (currently just by checking first character)if (_baseNameValidator ! null) {if (!_baseNameValidator.accept(c, basename, offset)) {return null;}}// next check: is the first character upper case? If not, return as ischar d Character.toLowerCase(c);if (c d) {return basename.substring(offset);}// otherwise, lower case initial chars. Common case first, just one charStringBuilder sb new StringBuilder(end - offset);sb.append(d);int i offset1;for (; i end; i) {c basename.charAt(i);d Character.toLowerCase(c);if (c d) {sb.append(basename, i, end);break;}sb.append(d);}return sb.toString();}分析得出 如果属性名第一个字母是小写第二个字母是大写如 qTitleJackson 会将整个属性名转为小写qtitle。这导致 JSON 里的 qTitle 和 Java 字段 qTitle 匹配不上。
解决方案
推荐避免使用单字母大写字母的属性名。 例如将 qTitle 改为 questionTitle。 如必须使用添加 JsonProperty 注解JsonProperty(qTitle)
private String qTitle;确保 Jackson 配置为默认大小写敏感命名策略为 LOWER_CAMEL_CASE。团队命名规范建议 尽量使用完整单词命名避免单字母大写字母的驼峰风格。
总结
本次 bug 的根本原因是 Jackson 对属性名的“遗留”处理逻辑遇到小写字母后紧跟大写字母的属性名时会将整个属性名转为小写导致前后端字段无法正确映射。 建议统一命名规范或使用 JsonProperty 明确指定字段名彻底规避此类问题。 文章转载自: http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.ymwnc.cn.gov.cn.ymwnc.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.xfxnq.cn.gov.cn.xfxnq.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.rdymd.cn.gov.cn.rdymd.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.plfy.cn.gov.cn.plfy.cn