pad*_*dis 9 java list jaxb map marshalling
我有一份我需要编组的列表地图.我创建了XML适配器,但java.util.List is an interface, and JAXB can't handle interfaces.
在创建JAXB上下文时我一直在努力.我该如何编组列表地图?
这是我的代码:
@XmlRootElement(name = "myClass")
public class MyClass {
@XmlJavaTypeAdapter(MapOfListsAdapter.class)
protected Map<Integer, List<Condition>> expectedResults;
Run Code Online (Sandbox Code Playgroud)
我为Map编写了适配器MapOfListsAdapater:
public class MapOfListsAdapter extends XmlAdapter<List<MapOfListsEntry>, Map<Integer, List<Condition>>> {
@Override
public List<MapOfListsEntry> marshal(Map<Integer, List<Condition>> v) {...}
@Override
public Map<Integer, List<Condition>> unmarshal(List<MapOfListsEntry> v) {...}
}
Run Code Online (Sandbox Code Playgroud)
MapOfListEntry具有以下JAXB注释:
public class MapOfListsEntry {
@XmlAttribute
private Integer key;
@XmlElementRef
@XmlElementWrapper
private List<Condition> value;
Run Code Online (Sandbox Code Playgroud)
我想到了。问题是我的适配器中的 ValueType 是 List,而这里的 List 是 JAXB 无法处理的类型。将此列表包装在另一个具体类(适配器中的 ValueType)中解决了问题。
适配器:
public class MapOfListsAdapter extends XmlAdapter<ListWrapper, Map<Integer, List<Condition>>> {
@Override
public ListWrapper marshal(Map<Integer, List<Condition>> v) {...}
@Override
public Map<Integer, List<Condition>> unmarshal(ListWrapper v) {...}
}
Run Code Online (Sandbox Code Playgroud)
包装清单:
public class ListWrapper {
@XmlElementRef
private List<MapOfListEntry> list;
Run Code Online (Sandbox Code Playgroud)