2008-11-18 166 views
40

我似乎得到以下异常尝试部署我的应用程序时:java.util.List的是一个接口,而JAXB无法处理接口

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 


我的代码工作得很好,直到我改变从列表中返回类型列出清单< <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) { 

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


我也尝试通过删除@SOAPBinding并尝试默认,但会发生相同的结果。 感谢所有帮助

UPDATE

我要指出了一些东西。我将所有List更改为ArrayList,然后编译。我说为什么编译而不工作是因为它表现奇怪。我得到一个类型为Object的对象: RelationServiceStub.ArrayList 但该对象没有get方法或者不像List那样工作。我也试图把它放到一个列表中,但没有奏效。

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

回答

24

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

相反,你需要定义其持有List<RelationCanonical>一个JAXB类型(我称之为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

8

如果适合你的目的,你总是可以这样定义一个数组:

YourType[] 

JAXB肯定能弄清楚,是什么,你应该能够立刻使用它的客户端。我也建议你这样做,因为你不应该能够通过列表修改从服务器检索的数组,但通过Web服务提供的方法

1

如果你想这样做的任何类。

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

您可以使用“的ArrayList”,而不是内部的“清单”

+1

是的,我在我做这个问题已经提到的,但它仍然没有奏效。阅读更新 – 2013-09-13 13:01:04

相关问题