@Accessors(chain = true)注解报错的解决方案

网友投稿 1061 2023-01-09

@Accessors(chain = true)注解报错的解决方案

@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

@Accehttp://ssors(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;

LWobtwthis.quantity = quantity;

}

}

}

lombok的@Accessors注解使用要注意

Accessors翻译是存取器。通过该注解可以控制getter和setter方法的形式。

特别注意如果不是常规的get|set,如使用此类配置(chain = true或者chain = true)。在用一些扩展工具会有问题,比如 BeanUtils.populate 将map转换为bean的时候无法使用。具体问题可以查看转换源码分析

@Accessors(fluent = true)#

使用fluent属性,getter和setter方法的方法名都是http://属性名,且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 voidhttp:// id(String id){...} //set

private String id(){} //get

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业app开发与推广(企业app开发制作公司)
下一篇:金融小程序试用场景分析(小程序融资方案)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~