@Accessors(chain = true)注解报错的解决方案
如下所示:
Cannot invoke setItemTitle(String) on the primitive type void
定义的实体类如下:
@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; } } }
这种写法不报错
request.getItems() .add(new RefundOrderItem(productPO.getName(), quantity));
这种写法报错
request.getItems() .add(new RefundOrderItem().setItemTitle(productPO.getName()).setQuantity(quantity)));
上述报错的解决方法如下:
在定义的实体类上加上注解:@Accessors(chain = true)
实体类代码如下:
@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; } } }
lombok的@Accessors注解使用要注意
Accessors翻译是存取器。通过该注解可以控制getter和setter方法的形式。
特别注意如果不是常规的get|set,如使用此类配置(chain = true或者chain = true)。在用一些扩展工具会有问题,比如 BeanUtils.populate 将map转换为bean的时候无法使用。具体问题可以查看转换源码分析
@Accessors(fluent = true)#
使用fluent属性,getter和setter方法的方法名都是属性名,且setter方法返回当前对象
class Demo{ private String id; private Demo id(String id){...} //set private String id(){} //get }
@Accessors(chain = true)#
使用chain属性,setter方法返回当前对象
class Demo{ private String id; private Demo setId(String id){...} //set private String id(){} //get }
@Accessors(prefix = "f")#
使用prefix属性,getter和setter方法会忽视属性名的指定前缀(遵守驼峰命名)
class Demo{ private String fid; private void id(String id){...} //set private String id(){} //get }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot中MyBatis-Plus 查询时排除某些字段的操作方法
这篇文章主要介绍了SpringBoot中MyBatis-Plus 查询时排除某些字段的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-08-08Java持久化框架Hibernate与Mybatis优劣及选择详解
这篇文章主要介绍了Java持久化框架Hibernate与Mybatis优劣及选择详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-05-05Java实现超大Excel文件解析(XSSF,SXSSF,easyExcel)
这篇文章主要为大家详细介绍了如何利用Java语言实现超大Excel文件解析(XSSF,SXSSF,easyExcel)以及速度的对比,感兴趣的可以了解一下2022-07-07SpringMVC中DispatcherServlet的HandlerMapping详解
这篇文章主要介绍了SpringMVC中DispatcherServlet的HandlerMapping详解,上回说的Handler,我们说是处理特定请求的,也就是说,不是所有的请求都能处理,那么问题来了,我们怎知道哪个请求是由哪个Handler处理的呢,需要的朋友可以参考下2023-10-10springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Fre
这篇文章主要介绍了springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-05-05
最新评论