为什么wsimport在使用@XmlRootElement注释的服务器对象时遇到问题? [英] Why does wsimport have trouble with server object having @XmlRootElement annotation?

查看:339
本文介绍了为什么wsimport在使用@XmlRootElement注释的服务器对象时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在服务器端使用JAX-WS完成Web服务的工作。在许多域对象中,我使用 @XmlRootElement 来帮助促进使用JAXB将XML文件解组到服务中。一切顺利,输出就是我期望看到的使用SoapUI。

I was completing work on a web service using JAX-WS on the server side. In many of the domain objects I used @XmlRootElement to help facilitate the unmarshaling of XML files into the service using JAXB. All went well and the output was what I expected to see using SoapUI.

然而,当我使用wsimport创建客户端时(作为其他开发人员的便利DAO),我开始在客户端集成测试类中遇到NullPointerExceptions。

However, when I used wsimport to create the client (as a convenience DAO for other developers), I started encountering NullPointerExceptions in my client integration-test class.

对webservice的调用工作正常,客户端收到响应,但是我的更复杂的对象是空的。像Strings这样的简单属性返回了大量可用数据,但不是更大的对象。

The call to the webservice worked correctly, and a response was received by the client, but my more-complex objects were null. Simple attributes, like Strings, were returning full of usable data, but not the larger objects.

通过使用简单的字符串重新创建服务并迁移到更复杂的对象的迭代,我发现当客户端收到使用 @XmlRootElement 在服务器上声明的对象时,这些对象是null。如果服务器对象没有 @XmlRootElement 注释,则客户端会收到所有复杂荣耀中的所有数据。

Through iterations of recreating services using simple Strings and migrating to more complex objects, I discovered that when the client received objects that were declared on the server with @XmlRootElement, these were the objects that were null. If the server object did not have the @XmlRootElement annotation, the client received all of the data in all of its complex glory.

最初缺少 @XmlRootElement 让我适合解组服务器上的数据,但这个答案帮助了我。

Initially the lack of @XmlRootElement gave me fits with unmarshaling the data on the server, but this answer helped me out.

因此,wsimport客户端因为解密Web服务响应而无声地失败的现象我担心 @XmlRootElement 注释(在服务器上!)。在这种情况下,我控制了双方,并可以做些什么。但是,如果我无法控制服务器怎么办?我将如何使用wsimport生成的代码解决这个问题?

So, the phenomenon of the wsimport client silently failing on the unmarshaling of the web service response because of the @XmlRootElement annotation (on the server!) has me concerned. In this case I had control of both sides and could do something about it. But, what if I don't have control of the server? How would I have resolved this with just the wsimport-generated code?

推荐答案

找到答案或原因,以为我会分享。

Found the answer, or the reason, so thought I would share.

@XmlRootElement 注释对于纯 JAXB 绑定很有用,但是当对象(以及生成的XML) )打包为 SOAP 响应它们可能与 WSDL 的表示完全匹配数据取决于其他注释的值。

The @XmlRootElement annotation is useful for plain JAXB bindings, but when the objects (and the resulting XML) are packaged as a SOAP response it is possible that they don't exactly match the WSDL's representation of the data depending on the value of other annotations.

在服务器上的类上使用 @XmlRootElement 注释由 @WebMethod 方法返回, WSDL 将包含一个元素定义,例如:

With the @XmlRootElement annotation on the class on the server which is being returned by a @WebMethod method, the WSDL will include an element definition such as:

<xs:element name="foo" type="tns:FooType"/>

然后在其他地方你的 WSDL 将包含一个参考按顺序排列的元素如下:

then elsewhere your WSDL will include a reference to the element in a sequence such as:

<xs:seqeunce>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="tns:foo"/>
</xs:sequence>

此引用是由 @XmlRootElement 引起的注释可能会混淆根元素声明的意图与SOAP响应的实际XML相比。

This referencing is caused by the @XmlRootElement annotation may confuse the intent of the root element declaration compared to the actual XML of the SOAP response.

相比之下, WSDL @XmlRootElement 注释的情况下生成的$ c>不包含< xs:element name =foo/> 声明。相反,它的元素被描述为:

In contrast, the WSDL generated without the @XmlRootElement annotation on the server objects does not contain a <xs:element name="foo"/> declaration at all. Rather its element is described as:

<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="foo" type="tns:FooType"/>
</xs:sequence>

这可能更好地匹配SOAP响应XML的表示方式以及将XML解组到类中由 wsimport 生成的工作正常。

This probably better matches the way the SOAP response XML is represented and the unmarshalling of the XML into the classes generated by wsimport works just fine.

如何使用 @XmlRootElement JAX-WS 服务中?

How to use @XmlRootElement in a JAX-WS service?

wsimport 似乎处理服务返回的XML有效性的某种程度的懒惰。我们吸取的教训是努力在 @WebResult上使用 name targetNamespace 描述您的Web服务方法的注释。 @XmlRootElement 注释需要匹配 targetNamespace 名称 >。当它们全部匹配时,解组按预期发生。当这些值不匹配时,由 wsimport 生成和注释的存根类将无法正确使用XML。

wsimport seems to handle some degree of laziness in the validity of the XML returned by a service. The lesson learned is to be diligent in your use of name and targetNamespace on your @WebResult annotations describing your web service method. The @XmlRootElement annotation needs to match the name within the targetNamespace. When they all match, the unmarshalling happens as expected. When those values do not match, your stubbed classes generated and annotated by wsimport will not be able to properly consume the XML.

这篇关于为什么wsimport在使用@XmlRootElement注释的服务器对象时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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