java.util.List is an interface, and JAXB can't handle interfaces

99封情书 提交于 2019-11-27 18:19:16
Henning

In my understanding, you will not be able to process a plain List via JAXB, as JAXB has no idea how to transform that into XML.

Instead, you will need to define a JAXB type which holds a List<RelationCanonical> (I'll call it Type1), and another one to hold a list of those types, in turn (as you're dealing with a List<List<...>>; I'll call this type Type2).

The result could then be an XML ouput like this:

<Type2 ...>
    <Type1 ...>
        <RelationCanonical ...> ... </RelationCanonical>
        <RelationCanonical ...> ... </RelationCanonical>
        ...
    </Type1>
    <Type1>
        <RelationCanonical ...> ... </RelationCanonical>
        <RelationCanonical ...> ... </RelationCanonical>
        ...
    </Type1>
    ...
</Type2>

Without the two enclosing JAXB-annotated types, the JAXB processor has no idea what markup to generate, and thus fails.

--Edit:

What I mean should look somewhat like this:

@XmlType
public class Type1{

    private List<RelationCanonical> relations;

    @XmlElement
    public List<RelationCanonical> getRelations(){
        return this.relations;
    }

    public void setRelations(List<RelationCanonical> relations){
        this.relations = relations;
    }
}

and

@XmlRootElement
public class Type2{

    private List<Type1> type1s;

    @XmlElement
    public List<Type1> getType1s(){
        return this.type1s;
    }

    public void setType1s(List<Type1> type1s){
        this.type1s= type1s;
    }
}

You should also check out the JAXB section in the J5EE tutorial and the Unofficial JAXB Guide.

If that suits your purpose you can always define an array like this:

YourType[]

JAXB can certainly figure out what that is and you should be able to immediatly use it client side. I would also recommend you to do it that way, since you should not be able to modify the array retrieved from a server via a List but via methods provided by the web service

If you want to do this for any class.

return items.size() > 0 ? items.toArray((Object[]) Array.newInstance(
            items.get(0).getClass(), 0)) : new Object[0];

You can use "ArrayList" instead of inner "List"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!