Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB marshaling Map of Lists

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;
like image 254
padis Avatar asked Jul 29 '11 09:07

padis


1 Answers

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;
like image 182
padis Avatar answered Oct 24 '22 10:10

padis