.png)
Cannot invoke setItemTitle(String) on the primitive type void
定义的实体类如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Data public static class RefundOrderItem implements Serializable { /** * 商品标题 */ @JsonProperty("item_title") private String itemTitle; /** * 数量 */ private BigDecimal quantity; public RefundOrderItem() { super(); } public RefundOrderItem(String itemTitle, BigDecimal quantity) { this.itemTitle = itemTitle; this.quantity = quantity; } }} |
这种写法不报错
1 2 | request.getItems().add(new RefundOrderItem(productPO.getName(), quantity)); |
这种写法报错
1 2 | request.getItems().add(new RefundOrderItem().setItemTitle(productPO.getName()).setQuantity(quantity))); |
在定义的实体类上加上注解:@Accessors(chain = true)
实体类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Data @Accessors(chain = true) public static class RefundOrderItem implements Serializable { /** * 商品标题 */ @JsonProperty("item_title") private String itemTitle; /** * 数量 */ private BigDecimal quantity; public RefundOrderItem() { super(); } public RefundOrderItem(String itemTitle, BigDecimal quantity) { this.itemTitle = itemTitle; this.quantity = quantity; } }} |
Accessors翻译是存取器。通过该注解可以控制getter和setter方法的形式。
特别注意如果不是常规的get|set,如使用此类配置(chain = true或者chain = true)。在用一些扩展工具会有问题,比如 BeanUtils.populate 将map转换为bean的时候无法使用。具体问题可以查看转换源码分析
使用fluent属性,getter和setter方法的方法名都是属性名,且setter方法返回当前对象
1 2 3 4 5 | class Demo{ private String id; private Demo id(String id){...} //set private String id(){} //get} |
使用chain属性,setter方法返回当前对象
1 2 3 4 5 | class Demo{ private String id; private Demo setId(String id){...} //set private String id(){} //get} |
使用prefix属性,getter和setter方法会忽视属性名的指定前缀(遵守驼峰命名)
1 2 3 4 5 | class Demo{ private String fid; private void id(String id){...} //set private String id(){} //get} |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
武汉格发信息技术有限公司,格发许可优化管理系统可以帮你评估贵公司软件许可的真实需求,再低成本合规性管理软件许可,帮助贵司提高软件投资回报率,为软件采购、使用提供科学决策依据。支持的软件有: CAD,CAE,PDM,PLM,Catia,Ugnx, AutoCAD, Pro/E, Solidworks ,Hyperworks, Protel,CAXA,OpenWorks LandMark,MATLAB,Enovia,Winchill,TeamCenter,MathCAD,Ansys, Abaqus,ls-dyna, Fluent, MSC,Bentley,License,UG,ug,catia,Dassault Systèmes,AutoDesk,Altair,autocad,PTC,SolidWorks,Ansys,Siemens PLM Software,Paradigm,Mathworks,Borland,AVEVA,ESRI,hP,Solibri,Progman,Leica,Cadence,IBM,SIMULIA,Citrix,Sybase,Schlumberger,MSC Products...