为什么JavaBeans不从List<?>类型序列化属性? [英] Why doesn't JavaBeans serialize a property from type List<?>?

查看:53
本文介绍了为什么JavaBeans不从List<?>类型序列化属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaBeans做一些简单的XML序列化,一个对象具有五个getter/setter属性和两个getter.这两个来自类型List< ...>:

I'm trying to do some simple XML serialization with JavaBeans, with an object having five getter/setter properties, and two getters. These two are from type 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?

I'm then using the XMLEncoder class (although here JAXB may be more appropriate, but I'm keeping it simple for now). The resulting XML has only the five getter/setter properties, the two properties of type List haven't been encoded. Is it because they are read only, or do I have to write a PersistenceDelegate for those kind of generic Lists ?

好的,我已经进行了更多的研究,最简单的解决方法,而又不着迷于编写自己的PersistenceDelegate似乎是创建包装器类:

Okay I've researched more, and the easiest way to solve this without going mad with writing its own PersistenceDelegate seems to be the creation of a wrapper class :

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就可以毫无问题地获取和设置只读属性.但是,如果您有更优雅的解决方案,请随时鸣叫...

That way, java Beans has no problems getting and setting the read-only properties. However if you have a more elegant solution, feel free to chime in...

推荐答案

经过测试和反思,看来我的解决方案在JavaBeans上下文中是最简单的,只需创建包装器类...

After testing and reflexion, it seems my solution is the easiest in the context of JavaBeans, simply create the wrapper class...

这篇关于为什么JavaBeans不从List&lt;?&gt;类型序列化属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆