我可以将对象类型作为@WebMethod 的参数吗?

发布于 2024-09-08 07:05:34 字数 406 浏览 2 评论 0 原文

我尝试创建一个具有 java.lang.Object 参数的 Web 方法,但收到类似于以下内容的错误: http://community.jboss.org/message/532500

一个人回答说: : “您只能传递 JAXB 支持的类型 Web 方法参数。 java.lang.Object 无法作为 Web 方法参数传递。”

事实上,当我将参数类型更改为 Object 以外的其他类型时,它起作用了。

如果我无法将 Object 作为 Web 方法参数传递,这背后的原因是什么以及如何处理当我想通过单个 Web 方法传递各种对象类型时,

如果可以我应该怎么做?

I've tried to create a web method which has parameter of java.lang.Object but I receive error similar to:
http://community.jboss.org/message/532500

One guy answered to this saying that:
"You can pass only JAXB supported types web method parameters.
java.lang.Object cannot be passed as web method parameter."

And indeed, when I changed the parameter type to something other than Object it worked.

If I cannot pass Object as web method parameter what is the reason behind this and how to handle situations when I want to pass various object types through a single web method?

If I can how should I do it?

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

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

发布评论

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

评论(2

清醇 2024-09-15 07:05:34

更好的方法是使用 JAXB 作为他在另一篇文章中收到的答案中概述的 OP——请参阅此处:http://myarch.com/create-jax-ws-service-in-5-分钟

A better way to do this would be to use JAXB as the OP outlined in the answer he recieved to an alternate post -- refer here: http://myarch.com/create-jax-ws-service-in-5-minutes.

纵山崖 2024-09-15 07:05:34

我不是一个热衷于 JAXB/JAX-WS 的人,但是:他可能是对的。

为此,我们在内部使用的一种模式是手动序列化任何对象,因为当您跨 Web 服务边界发送对象时,您基本上将它们扁平化为仅用 WS-I 配置文件的词汇表即可表达的内容您尝试发送并通过调用者和服务理解的枚举为反序列化过程提供一些上下文。

在下面的简单示例中,DemuxEnum 将是一个枚举,其中包含您要发送的所有类型的值:

MyObject obj = new MyObject();
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOutStream);       
objectOut.writeObject(obj);
objectOut.close();

byte[] serializedObject= byteOutStream.toByteArray();
someWSObject.SendObject(DemuxEnum.MyObjectType, serializedObject);

您已经设置了一个采用 (DemuxEnum,字节[])。为了获得奖励积分,您可以使用替代序列化器/反序列化器技术,例如 Apache ThriftGoogle ProtocolBuffers

编辑:这样做的明显缺点是,如果它是面向客户的 Web 服务,那就不会令人兴奋。如果是这种情况,您可能最好枚举具有具体参数的 Web 方法。

I'm not a huge JAXB/JAX-WS guy, but: he's probably right.

One pattern we use internally for this--since when you're sending objects across a web service boundary, you're basically flattening them into something that's expressible solely with the vocabulary of your WS-I profile--is to manually serialize whatever object you're attempting to send, and provide some context for the process of deserialization via an enum understood by the caller and the service.

In the following simple example, DemuxEnum would be an enum that contains values for all of the types you want to send:

MyObject obj = new MyObject();
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteOutStream);       
objectOut.writeObject(obj);
objectOut.close();

byte[] serializedObject= byteOutStream.toByteArray();
someWSObject.SendObject(DemuxEnum.MyObjectType, serializedObject);

Where you've already set up a web service endpoint that takes (DemuxEnum, byte[]). For bonus points, you can toss in alternate serializer/deserializer technologies like Apache Thrift or Google ProtocolBuffers.

Edit: The obvious downside of this is that if it's a customer-facing web service, that's just not going to jive. You're probably better off enumerating web methods that have concrete parameters, if that's the case.

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