JAXB - 如何序列化集合的子类中的字段 [英] JAXB - How to serialize fields in a subclass of a collection

查看:238
本文介绍了JAXB - 如何序列化集合的子类中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法序列化我的类中的字段,这是ArrayList的子类。添加到列表中的项目是序列化的,但字段不是:

I'm having trouble serializing the fields in my class, which is a subclass of ArrayList. The items added to the list are serialized, but the fields aren't:

@XmlRootElement
public static class NumberedList extends ArrayList<String>{

    @XmlAttribute
    private int number = 5;

    @XmlList
    public List<String> getNames(){
        return this;
    }

    public NumberedList(){
        add("a");
        add("b");
    }
}

@XmlRootElement
public static class FieldTest{

    @XmlElement
    NumberedList list = new NumberedList();
}

public static void main(String[] args) throws Exception{

    JAXBContext context = JAXBContext.newInstance(FieldTest.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(new FieldTest(), System.out);
}

//将NumberedList序列化为字段的输出:不存在数字字段

//Output from serializing the NumberedList as a field: no number field is present

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><fieldTest><list>a</list><list>b</list></fieldTest>

奇怪的是,当我将列表添加到Map时,我班级中的字段会被序列化正如我所期望的那样。

The strange thing is that when I add the list to a Map, the fields in my class get serialized as I expect them to.

@XmlRootElement
public static class NumberedList extends ArrayList<String>{

    @XmlAttribute
    private int number = 5;

    @XmlList
    public List<String> getNames(){
        return this;
    }

    public NumberedList(){
        add("a");
        add("b");
    }
}

@XmlRootElement
public static class MapTest{
    @XmlElement
    Map<Integer, NumberedList> map = Maps.newHashMap();

    public MapTest(){
        map.put(1, new NumberedList());
    }
}

public static void main(String[] args) throws Exception{

    JAXBContext context = JAXBContext.newInstance(MapTest.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(new MapTest(), System.out);
}

//在地图中序列化NumberedList的输出:

//Output from serializing the NumberedList in a Map:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><mapTest><map><entry><key>1</key><value number="5"><names>a b</names></value></entry></map></mapTest>

任何想法?

推荐答案

问题来自于 NumberedList 正在扩展 ArrayList 。向类中添加内部 ArrayList ,然后将其与 NumberedList 中存在的其他字段一起序列化。请参阅以下代码:

The problem comes from the fact that NumberedList is extending ArrayList. Add an internal ArrayList to the class and then serialize it along with other fields that exist in NumberedList. See the below code:

@XmlRootElement
public static class NumberedList {
    private List<String> names = new ArrayList<String>();

    @XmlAttribute
    private int number = 5;

    public NumberedList(){
        names.add("a");
        names.add("b");
    }

    @XmlList
    public List<String> getNames(){
        return names;
    }
}

这篇关于JAXB - 如何序列化集合的子类中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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