java.util.List是一个接口,JAXB无法处理接口

She*_*ari 45 java web-services jaxb jbossws java-ee

尝试部署我的应用程序时,我似乎遇到以下异常:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of     IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
    at java.util.List
    at private java.util.List     foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return
    at     foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse
java.util.List does not have a no-arg default constructor.
    this problem is related to the following location:
        at java.util.List
        at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return
    at     foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse
Run Code Online (Sandbox Code Playgroud)


我的代码工作得很好,直到我将返回类型从List更改为List <List <RelationCanonical >>

这是部分Web服务:


@Name("relationService")
@Stateless
@WebService(name = "RelationService", serviceName = "RelationService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class RelationService implements RelationServiceLocal {

    private boolean login(String username, String password) {
        Identity.instance().setUsername(username);
        Identity.instance().setPassword(password);
        Identity.instance().login();
        return Identity.instance().isLoggedIn();
    }

    private boolean logout() {
        Identity.instance().logout();
        return !Identity.instance().isLoggedIn();
    }

    @WebMethod
    public List<List<RelationCanonical>> getRelationsFromPerson(@WebParam(name = "username")
    String username, @WebParam(name = "password")
    String password, @WebParam(name = "foedselsnummer")
    String... foedselsnummer) {

......
......
......
}
Run Code Online (Sandbox Code Playgroud)


我还尝试删除@SOAPBinding并尝试默认,但会出现相同的结果.感谢任何帮助

UPDATE

我想要注意一些事情.我将所有List更改为ArrayList,然后编译.我说编译而不工作的原因是因为它表现得很奇怪.我得到一个类型的对象:RelationServiceStub.ArrayList但该对象没有get方法或者也不像List一样.我也尝试将它投射到List但是没有用.

请注意,这是在我使用Axis 2和wsdl2java之后所以是的,现在它编译,但我不知道如何获取数据.

Hen*_*ing 26

根据我的理解,您将无法List通过JAXB 处理plain ,因为JAXB不知道如何将其转换为XML.

相反,你需要定义一个JAXB类型,它包含一个List<RelationCanonical>(我称之为Type1),另一个用于保存这些类型的列表(因为你正在处理一个List<List<...>>;我会称之为这个类型Type2) .

结果可能是这样的XML输出:

<Type2 ...>
    <Type1 ...>
        <RelationCanonical ...> ... </RelationCanonical>
        <RelationCanonical ...> ... </RelationCanonical>
        ...
    </Type1>
    <Type1>
        <RelationCanonical ...> ... </RelationCanonical>
        <RelationCanonical ...> ... </RelationCanonical>
        ...
    </Type1>
    ...
</Type2>
Run Code Online (Sandbox Code Playgroud)

如果没有两个带有JAXB注释的类型,JAXB处理器就不知道要生成什么标记,因此会失败.

- 编辑:

我的意思应该看起来像这样:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您还应该查看J5EE教程非官方JAXB指南中JAXB部分.


小智 8

如果这符合您的目的,您始终可以定义如下数组:

YourType[]
Run Code Online (Sandbox Code Playgroud)

JAXB当然可以弄清楚它是什么,你应该能够立即使用它的客户端.我还建议你这样做,因为你不能通过List修改从服务器检索的数组,但是通过Web服务提供的方法