我正在尝试使用JavaBeans进行一些简单的XML序列化,其中一个对象具有五个getter / setter属性,以及两个getter。这两个来自类型List< ...> :
public List<MasterDataQueryMDType> getType() {
if (type == null) {
type = new ArrayList<MasterDataQueryMDType>();
}
return this.type;
}
和
public List<MasterDataQueryKey> getKey() {
if (key == null) {
key = new ArrayList<MasterDataQueryKey>();
}
return this.key;
}
然后我使用XMLEncoder类(虽然这里JAXB可能更合适,但我现在保持简单)。生成的XML只有五个getter / setter属性,List类型的两个属性尚未编码。是因为它们是只读的,还是我必须为这些通用列表编写PersistenceDelegate?
好的,我已经研究了更多,解决这个问题的最简单方法是编写自己的PersistenceDelegate而不是生气似乎是创建了一个包装类:
public class MasterDataQueryWrapper {
private MasterDataQuery query;
public MasterDataQuery getQuery(){
return this.query;
}
public void setQuery(MasterDataQuery value){
this.query = value;
}
public List<MasterDataQueryMDType> getType(){
return query.getType();
}
public void setType(List<MasterDataQueryMDType> value){
for (MasterDataQueryMDType t:value){
this.query.getType().add(t);
}
}
public List<MasterDataQueryKey> getKey(){
return query.getKey();
}
public void setKey(List<MasterDataQueryKey> value){
for (MasterDataQueryKey k:value){
this.query.getKey().add(k);
}
}
}
这样,java Beans在获取和设置只读属性方面没有任何问题。但是,如果您有更优雅的解决方案,请随意加入......
答案 0 :(得分:0)
经过测试和反思,似乎我的解决方案在JavaBeans环境中最简单,只需创建包装类......