java.util.List是一个接口,JAXB无法处理接口 [英] java.util.List is an interface, and JAXB can't handle interfaces

查看:381
本文介绍了java.util.List是一个接口,JAXB无法处理接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I seemed to get the following exception when trying to deploy my application:

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>>

以下是部分Web服务:

Here is the partial webservice:


@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但是没有用。

I want to note out something. I changed all List to ArrayList, and then it compiled. The reason why I say compiled and not worked is because it behaves strange. I get an Object of type: RelationServiceStub.ArrayList but the object has no get methods or does not behave as a List either. I also tried to cast it to a List but that didnt work.

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

Note that this is after I have used Axis 2 and wsdl2java So yes, now it compiles, but I dont know how to get the data out.

推荐答案

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

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.

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

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).

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

The result could then be an XML ouput like this:

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

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

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

- 编辑:

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

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

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

你还应该看看 J5EE教程中的JAXB部分非官方的JAXB指南

这篇关于java.util.List是一个接口,JAXB无法处理接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆