您好,登錄后才能下訂單哦!
@Accessors注解出現報錯如何解決,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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; } } }
Accessors翻譯是存取器。通過該注解可以控制getter和setter方法的形式。
特別注意如果不是常規(guī)的get|set,如使用此類配置(chain = true或者chain = true)。在用一些擴展工具會有問題,比如 BeanUtils.populate 將map轉換為bean的時候無法使用。具體問題可以查看轉換源碼分析
使用fluent屬性,getter和setter方法的方法名都是屬性名,且setter方法返回當前對象
class Demo{ private String id; private Demo id(String id){...} //set private String id(){} //get }
使用chain屬性,setter方法返回當前對象
class Demo{ private String id; private Demo setId(String id){...} //set private String id(){} //get }
使用prefix屬性,getter和setter方法會忽視屬性名的指定前綴(遵守駝峰命名)
class Demo{ private String fid; private void id(String id){...} //set private String id(){} //get }
關于@Accessors注解出現報錯如何解決問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。