@Accessors(chain = true)注解報(bào)錯(cuò)的解決方案
如下所示:
Cannot invoke setItemTitle(String) on the primitive type void
定義的實(shí)體類如下:
@Data
public static class RefundOrderItem implements Serializable {
/**
* 商品標(biāo)題
*/
@JsonProperty("item_title")
private String itemTitle;
/**
* 數(shù)量
*/
private BigDecimal quantity;
public RefundOrderItem() {
super();
}
public RefundOrderItem(String itemTitle, BigDecimal quantity) {
this.itemTitle = itemTitle;
this.quantity = quantity;
}
}
}
這種寫(xiě)法不報(bào)錯(cuò)
request.getItems() .add(new RefundOrderItem(productPO.getName(), quantity));
這種寫(xiě)法報(bào)錯(cuò)
request.getItems() .add(new RefundOrderItem().setItemTitle(productPO.getName()).setQuantity(quantity)));
上述報(bào)錯(cuò)的解決方法如下:
在定義的實(shí)體類上加上注解:@Accessors(chain = true)
實(shí)體類代碼如下:
@Data
@Accessors(chain = true)
public static class RefundOrderItem implements Serializable {
/**
* 商品標(biāo)題
*/
@JsonProperty("item_title")
private String itemTitle;
/**
* 數(shù)量
*/
private BigDecimal quantity;
public RefundOrderItem() {
super();
}
public RefundOrderItem(String itemTitle, BigDecimal quantity) {
this.itemTitle = itemTitle;
this.quantity = quantity;
}
}
}
lombok的@Accessors注解使用要注意
Accessors翻譯是存取器。通過(guò)該注解可以控制getter和setter方法的形式。
特別注意如果不是常規(guī)的get|set,如使用此類配置(chain = true或者chain = true)。在用一些擴(kuò)展工具會(huì)有問(wèn)題,比如 BeanUtils.populate 將map轉(zhuǎn)換為bean的時(shí)候無(wú)法使用。具體問(wèn)題可以查看轉(zhuǎn)換源碼分析
@Accessors(fluent = true)#
使用fluent屬性,getter和setter方法的方法名都是屬性名,且setter方法返回當(dāng)前對(duì)象
class Demo{
private String id;
private Demo id(String id){...} //set
private String id(){} //get
}
@Accessors(chain = true)#
使用chain屬性,setter方法返回當(dāng)前對(duì)象
class Demo{
private String id;
private Demo setId(String id){...} //set
private String id(){} //get
}
@Accessors(prefix = "f")#
使用prefix屬性,getter和setter方法會(huì)忽視屬性名的指定前綴(遵守駝峰命名)
class Demo{
private String fid;
private void id(String id){...} //set
private String id(){} //get
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java動(dòng)態(tài)線程池插件dynamic-tp集成zookeeper
ZooKeeper是一個(gè)分布式的,開(kāi)放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開(kāi)源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等2023-03-03
SpringMVC的@InitBinder參數(shù)轉(zhuǎn)換代碼實(shí)例
這篇文章主要介紹了SpringMVC的@InitBinder參數(shù)轉(zhuǎn)換代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java Integer[]和int[]互相轉(zhuǎn)換方式
這篇文章主要介紹了Java Integer[]和int[]互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實(shí)例詳解
這篇文章主要介紹了基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實(shí)例詳解,需要的朋友可以參考下2017-04-04

