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

发布于 2024-07-08 09:29:38 字数 2329 浏览 9 评论 0 原文

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

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>

这是部分内容网络服务:


@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。 我还尝试将其转换为列表,但没有成功。

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

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


My code worked just well until I changed the return type from List to List<List<RelationCanonical>>

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

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

I have also tried by removing the @SOAPBinding and trying default, but the same result occurs.
Appreciate any help

UPDATE

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尐偏执 2024-07-15 09:29:38

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

相反,您需要定义一个包含 List 的 JAXB 类型(我将其称为 Type1),以及另一个包含这些类型列表的 JAXB 类型,依次(当您处理 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;
    }
}

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

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.

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

The result could then be an XML ouput like this:

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

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

--Edit:

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

and

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

You should also check out the JAXB section in the J5EE tutorial and the Unofficial JAXB Guide.

夏末的微笑 2024-07-15 09:29:38

如果这适合您的目的,您始终可以像这样定义一个数组:

YourType[]

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

If that suits your purpose you can always define an array like this:

YourType[]

JAXB can certainly figure out what that is and you should be able to immediatly use it client side. I would also recommend you to do it that way, since you should not be able to modify the array retrieved from a server via a List but via methods provided by the web service

自此以后,行同陌路 2024-07-15 09:29:38

如果您想为任何课程执行此操作。

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

If you want to do this for any class.

return items.size() > 0 ? items.toArray((Object[]) Array.newInstance(
            items.get(0).getClass(), 0)) : new Object[0];
_蜘蛛 2024-07-15 09:29:38

我收到的错误消息与您的错误消息完全相同(法语):

java.util.List 是一个接口,与 JAXB 无关
接口

遇到了同样的问题,我在 Web 方法的声明中返回了一个 java.util.List ,我得到了一个 java.util.List 在客户端代码中,我只需调用 RelationCanonicalArray.getItem() 即可获取 java.util.List。 我使用 Jakarta EE 9.1 和 Wildfly 26.1.1 进行了测试。 Henning 的答案是正确的,但是当 JAXB 已经支持 RelationCanonical 时,为什么还要让事情变得如此复杂呢?

I had exactly the same error message than yours (in French):

java.util.List est une interface et JAXB ne peut pas gérer les
interfaces

I had the same problem and I returned a java.util.List<RelationCanonical[]> in the declaration of the web method, I got a java.util.List<RelationCanonicalArray> in the client side code and I just had to call RelationCanonicalArray.getItem() to get a java.util.List<RelationCanonical>. I tested with Jakarta EE 9.1 with Wildfly 26.1.1. Henning's answer is correct but when RelationCanonical is already supported by JAXB, why driving things so much more complicated?

贱人配狗天长地久 2024-07-15 09:29:38

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

You can use "ArrayList" instead of inner "List"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文