【问题标题】:Generating Java Source from an XSD that defines SOAP messages从定义 SOAP 消息的 XSD 生成 Java 源
【发布时间】:2012-04-20 19:34:57
【问题描述】:

我有一个为远程设备定义 SOAP 消息的 XSD。我想使用这个 XSD 并从中生成 Java 源代码来创建这些消息。 XSD 中的一条消息的示例如下:

<xs:element name="get-ethernet-stats">
    <xs:annotation>
        <xs:documentation>Ethernet stats information</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="mode" type="response-mode"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="get-ethernet-stats-resp">
    <xs:complexType>
        <xs:annotation>
            <xs:documentation>Ethernet Stats</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="status"/>
            <xs:element name="result" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="rx" type="xs:int"/>
                        <xs:element name="tx" type="xs:int"/>
                        <xs:element name="drops" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

关于如何做到这一点的任何建议?我查看了 JAXB,但没有原始源文件,当我运行“xjc file.xsd”来获取源文件时,这里是输出的一部分:

parsing a schema...
compiling a schema...
[ERROR] A class/interface with the same name "file.MemosReceipt" is already in use. Use a class customization to resolve this conflict.
  line 7669 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] (Relevant to above error) another "MemosReceipt" is generated from here.
  line 8242 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] A class/interface with the same name "file.InstructionsReceipt" is already in use. Use a class customization to resolve this conflict.
  line 7720 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] (Relevant to above error) another "InstructionsReceipt" is generated from here.
  line 8341 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 7686 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 8267 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 8626 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 6114 of file:/Users/rwb7041/Documents/file/xsd/file.xsd.xsd

Failed to produce code.

【问题讨论】:

    标签: java soap xsd


    【解决方案1】:

    我建议你看看jaxws-rt-2.1.4.jar。这允许通过两种方式提供要在 SOAP 消息中发送的 XML:
    (1) 只提供正文内容;该代码负责根据检索到的 wsdl 添加 Envelope/Header/Body 以及命名空间
    (2) 提供完整的信封,加上命名空间

    那么就不需要生成代码了。

    例如见jaxws-guide.html

    【讨论】:

    • 我不明白这有什么帮助。我知道它将帮助我生成 SOAP 信封,但根据我的理解,我将不得不生成正文。为了生成正文,我必须知道要发送什么消息,并且我需要从 XSD 生成适当的类来保存正文的内容
    • 嗯,不知道你到底在找什么,但我使用它的方式是创建一个新的 DOM 文档,根据 XSD 规范和具体细节填充它(代码生成在这里可能会有所帮助,但是为什么要麻烦),然后将 DOM 字符串化为 XML 并将其传递给 javax.xml.ws.Service 的新实例,将端口、服务、XML 和 WSDL url 提供给 createDispatch 方法。
    • 我没有 WSDL 文档,所以这根本没有帮助
    • 通常,Web 服务本身会为您提供 WSDL。它的 url 或多或少是 webservice url + .asmx?wsdl。例如https://www.eway.com.au/gateway/rebill/test/manageRebill_test.asmx?wsdl.
    • 它不是一个网络服务,它是一个我想要控制的远程设备。没有 WSDL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多