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

时间:2008-11-18 13:39:59

标签: java web-services java-ee jaxb jbossws

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

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


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

以下是部分网络服务:


@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) {

......
......
......
}


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

更新

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

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

4 个答案:

答案 0 :(得分:25)

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

相反,您需要定义一个JAXB类型,其中包含List<RelationCanonical>(我将其称为Type1),另一个用于保存这些类型的列表(就像您一样) “正在处理List<List<...>>;我会将此类型称为Type2)。

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

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

如果没有两个附加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;
    }
}

@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;
    }
}

您还应该查看JAXB section in the J5EE tutorialUnofficial JAXB Guide

答案 1 :(得分:8)

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

YourType[]

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

答案 2 :(得分:1)

如果你想为任何一个班级做这个。

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

答案 3 :(得分:-5)

您可以使用“ArrayList”代替内部“List”

相关问题