BeanUtils.copyProperties 屬性複製List的坑
阿新 • • 發佈:2018-12-21
BeanUtils.copyProperties 大家都知道是用來作為屬性複製的工具,最近在專案遇到了一個坑
class A{ private String name; private List<Cat> cats; private String age; } class Cat{ private String color; private String price; private String count; } class A1{ private String name; private List<Cat> cats; } class Cat1{ private String color; private String price; } //開始複製 A a=new A(); a.setName("cd"); a.setAge("11"); Cat cat=new Cat(); cat.setColor("red"); cat.setPrice("200"); cat.setCount("20"); a.setCat(cat); A1 a1=new A1(); BeanUtils.copyProperties(a1,a); //我們想得到的a1為 a1:{"name":"cd","cat":{"price":"200","color":"red"}} //實際的結果為 a1:{"name":"cd","cat":{"price":"200","color":"red","count":"20"}} //所以大家進行屬性複製的時候注意List,當有List的時候我們要取出來一個一個複製