如何在JAXB XmlAdapter中使用通用对象List [英] How to use generic object List in JAXB XmlAdapter
问题描述
我有两个类似的类,只有一个字符串名称。我想在列表中添加一个XML适配器,以便我可以从每个列表中获取一个CSV字符串。
I have two similar classes, that simply have a string name. I wanted to add an XML adapter to the Lists so that I could get a CSV string from each of them.
我试图像这样声明我的适配器:
I tried to declare my adapter like so:
public class ListXmlAdapter extends XmlAdapter<String, ArrayList<Object>> {
但是当我尝试使用它时,我得到非法的注释异常,说ListXmlAdapter不适用于任何类型我尝试传入注释 - 例如下面的测试
But when I try to use this I get illegal annotation exceptions saying ListXmlAdapter is not applicable for whatever type I try to pass in at the annotation - such as Test below
@XmlElement
@XmlJavaTypeAdapter(ListXmlAdapter.class)
List<Test> tests = new ArrayList<Test>()
我如何创建我想要用于的适配器我有几个不同对象类型的不同ArrayLists?
How would I create this adapter that I want to use for a few different ArrayLists of different object types I have?
推荐答案
如果将属性类型更改为ArrayList,它将起作用:
If you change the property type to ArrayList it will work:
@XmlElement
@XmlJavaTypeAdapter(ListXmlAdapter.class)
ArrayList<Test> tests = new ArrayList<Test>();
这篇关于如何在JAXB XmlAdapter中使用通用对象List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!