java.util.List是一个接口,JAXB不能处理接口
尝试部署我的应用程序时,我似乎得到以下exception:
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的返回types更改为List >
这是部分的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> getRelationsFromPerson(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "foedselsnummer") String... foedselsnummer) { ...... ...... ...... }
我也尝试通过删除@SOAPBinding并尝试默认,但同样的结果发生。 感谢任何帮助
UPDATE
我想注意一些事情。 我改变了所有List到ArrayList,然后编译。 我之所以说编译而不工作是因为它performance奇怪。 我得到一个types为RelationServiceStub.ArrayList的对象,但该对象没有get方法,或者不像List那样工作。 我也试图把它列入一个名单,但没有奏效。
请注意,这是我使用了Axis 2和wsdl2java之后所以是的,现在编译,但我不知道如何获取数据。
根据我的理解,您将无法通过JAXB处理简单的List ,因为JAXB不知道如何将其转换为XML。
相反,您将需要定义一个JAXBtypes,它包含一个List (我将它称为Type1 ),另一个保存这些types的列表(因为您正在处理List> ;我将称这种typesType2 )。
结果可能是这样的XML输出:
... ... ... ... ... ... ...
如果没有两个JAXB注释types,则JAXB处理器不知道要生成什么标记,从而失败。
– 编辑:
我的意思应该看起来像这样:
@XmlType public class Type1{ private List relations; @XmlElement public List getRelations(){ return this.relations; } public void setRelations(List relations){ this.relations = relations; } }
和
@XmlRootElement public class Type2{ private List type1s; @XmlElement public List getType1s(){ return this.type1s; } public void setType1s(List type1s){ this.type1s= type1s; } }
您还应该查看J5EE教程和非官方JAXB指南中的JAXB部分 。
如果这符合你的目的,你总是可以像这样定义一个数组:
YourType[]
JAXB当然可以弄清楚这是什么,你应该能立即使用它的客户端。 我也build议你这样做,因为你不应该能够通过列表修改从服务器检索的数组,但通过Web服务提供的方法
如果你想为任何课程做到这一点。
return items.size() > 0 ? items.toArray((Object[]) Array.newInstance( items.get(0).getClass(), 0)) : new Object[0];
你可以使用“ArrayList”而不是内部的“List”