JAXB 编组列表映射
我有一张需要整理的列表地图。我创建了 XML 适配器,但在创建 JAXB 上下文时,我不断收到 java.util.List is an interface, and JAXB can't handle faces.
消息。我应该如何整理列表地图?
这是我的代码:
@XmlRootElement(name = "myClass")
public class MyClass {
@XmlJavaTypeAdapter(MapOfListsAdapter.class)
protected Map<Integer, List<Condition>> expectedResults;
我已经为 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) {...}
}
MapOfListEntry 具有这些 JAXB 注释:
public class MapOfListsEntry {
@XmlAttribute
private Integer key;
@XmlElementRef
@XmlElementWrapper
private List<Condition> value;
I have a map of lists that I need to marshal. I created XML adapater but I keep getting java.util.List is an interface, and JAXB can't handle interfaces.
when creating JAXB context. How should I marshal Map of Lists?
This is my code:
@XmlRootElement(name = "myClass")
public class MyClass {
@XmlJavaTypeAdapter(MapOfListsAdapter.class)
protected Map<Integer, List<Condition>> expectedResults;
I have written adapter MapOfListsAdapater for the Map:
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) {...}
}
MapOfListEntry has these JAXB annotations:
public class MapOfListsEntry {
@XmlAttribute
private Integer key;
@XmlElementRef
@XmlElementWrapper
private List<Condition> value;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。问题是我的适配器中的 ValueType 是 List,而这里的 List 是 JAXB 无法处理的类型。将此列表包装在另一个具体类(适配器中的 ValueType)中解决了问题。
适配器:
包装清单:
I figured it out. The problem was that ValueType in my adapter was List and this List here was the type that JAXB could not handle. Wrapping this List in another concrete class that is ValueType in adapter solved the problem.
Adapter:
Wrapped list: