从 wsdl 创建客户端时缺少 @XmlRootElement [英] Missing @XmlRootElement when creating a client from a wsdl

查看:57
本文介绍了从 wsdl 创建客户端时缺少 @XmlRootElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用基于第三方 wsdl 文件的网络服务的问题.

I have a question in regards to consuming a web service based on a third party wsdl file.

我已经使用了给定的 wsdl 并生成了 120 多个所需的 java 文件.这个过程是通过使用 xjc 完成的.在 Sping 环境中,我能够通过调用几个公开的服务成功创建几个 JUnit 测试.

I've taken the given wsdl and generated the 120+ java files required. This process was done by using xjc. Within the Sping environment, I was able to successfully create a couple of JUnit tests by calling a couple of the exposed services.

但是,为了成功测试这些服务,我必须将 @XmlRootElement 注释添加到生成的 java 文件中.否则,我会遇到一个错误,说明

But, in order to successfully test those services I had to add the @XmlRootElement annotation to the generated java files. Otherwise, I would encounter an error stating

"com.sun.istack.SAXException2: 无法编组类型com.beam.services.client.UserGetRequestData"作为一个元素,因为它缺少@XmlRootElement 注释"

"com.sun.istack.SAXException2: unable to marshal type "com.beam.services.client.UserGetRequestData" as an element because it is missing an @XmlRootElement annotation"

.

我已经用尽了我的搜索……我无法控制 wsdl 文件是如何创建/结构化的.如何生成 java 文件以确保包含 @XmlRootElement 注释或编写客户端代码以避免上述错误?

I've exhausted my search… I have no control as to how the wsdl file is created/structured. How can I go about generating the java files to ensure that the @XmlRootElement annotation is included or go about writing the client side code in way to avoid the error above?

谢谢.

推荐答案

如果您确实需要@XmlRootElement,如果您的类型仅用于一个元素,则可以使用简单绑定模式.JAXB 默认不包含注解的原因以及如何使用简单绑定解释如下:https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-并非总是:

If you really need the @XmlRootElement you could use simple binding mode if your type is used for only one element. The reason why JAXB does not include the annotation by default and how to use simple binding is explained here: https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always:

您的架构可能被 XJC 未编译的其他架构使用现在

your schema might be used by other schemas that XJC isn't compiling right now

规范中没有定义这样的概念,但作为一个实验,我们有XJC中这种激进的优化模式,暂称为头脑简单的绑定模式".

Such notion isn't defined in the spec, but as an experiment we have such aggressive optimization mode in XJC, tentatively called "simple-minded binding mode".

移动博客时,样本似乎丢失了,但看起来像这样:

The sample seems to got lost when the moved the blog, but it looked like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionbindingprefixes="xjc">
  <xs:annotation>
    <xs:appinfo>
      <jaxb:globalbindings>
        <xjc:simple/>
      </jaxb:globalbindings>
    </xs:appinfo>
  </xs:annotation>      
  <xs:element name="foo" type="bar"/>
  <xs:complextype name="bar"/>
</xs:schema>

另一种可能是将其包装在 JAXBElement 中.ObjectFactory 应包含用于创建这些包装对象的方法.

The other possibilty is to wrap it in a JAXBElement. The ObjectFactory should include a method for creating these wrapped objects.

这篇关于从 wsdl 创建客户端时缺少 @XmlRootElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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