无法从 JAX-WS Web 服务返回集合或数组

发布于 2024-09-13 07:31:18 字数 643 浏览 3 评论 0原文

我发现我无法从 JAX-WS Web 服务返回集合。

我意识到 Java Collections API 可能并不被所有客户端支持,所以我转而返回一个数组,但我似乎也无法做到这一点。

我的 Web 服务设置如下:

@WebService
public class MyClass {
  public ReturnClass[] getArrayOfStuff() {
    // extremely complex business logic... or not
    return new ReturnClass[] {new ReturnClass(), new ReturnClass()};
  }
}

ReturnClass 只是一个 POJO。我创建了另一个返回单个实例的方法,并且该方法有效。当我使用集合/数组时,这似乎是一个问题。

当我部署该服务时,在使用它时出现以下异常:

javax.xml.bind.MarshalException - 带有链接异常: [javax.xml.bind.JAXBException:[LReturnClass;在此上下文中未知]

我是否需要以某种方式注释 ReturnClass 类以使 JAX-WS 意识到它? 还是我做错了什么?

I found that I was unable to return collections from my JAX-WS Web Service.

I appreciate that the Java Collections API may not be supported by all clients, so I switched to return an array, but I can't seem to do this either.

I've set up my web service as follows:

@WebService
public class MyClass {
  public ReturnClass[] getArrayOfStuff() {
    // extremely complex business logic... or not
    return new ReturnClass[] {new ReturnClass(), new ReturnClass()};
  }
}

And the ReturnClass is just a POJO. I created another method that returns a single instance, and that works. It just seems to be a problem when I use collections/arrays.

When I deploy the service, I get the following exception when I use it:

javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: [LReturnClass; is not known to this context]

Do I need to annotate the ReturnClass class somehow to make JAX-WS aware of it?
Or have I done something else wrong?

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

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

发布评论

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

评论(1

何以笙箫默 2024-09-20 07:31:18

我不确定这是否是正确的方法,但在一种情况下,我想返回一个集合,我将集合包装在另一个类中:

@WebService
public class MyClass {
    public CollectionOfStuff getArrayOfStuff() {
        return new CollectionOfStuff(new ReturnClass(), new ReturnClass());
    }
}

然后:

public class CollectionOfStuff {
   // Stuff here
   private List<ReturnClass> = new ArrayList<ReturnClass>();
   public CollectionOfStuff(ReturnClass... args) {
       // ...
   }
}

免责声明:我面前没有实际的代码,所以我想我的示例缺少一些注释等,但这就是要点。

I am unsure of wheter this is the correct way to do it, but in one case where I wanted to return a collection I wrapped the collection inside another class:

@WebService
public class MyClass {
    public CollectionOfStuff getArrayOfStuff() {
        return new CollectionOfStuff(new ReturnClass(), new ReturnClass());
    }
}

And then:

public class CollectionOfStuff {
   // Stuff here
   private List<ReturnClass> = new ArrayList<ReturnClass>();
   public CollectionOfStuff(ReturnClass... args) {
       // ...
   }
}

Disclaimer: I don't have the actual code in front of me, so I guess my example lacks some annotations or the like, but that's the gist of it.

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