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;
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:
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) {...}
}
Wrapped list:
public class ListWrapper {
@XmlElementRef
private List<MapOfListEntry> list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With