Java 从 xsd 创建一个 web 服务请求 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19527222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:02:22  来源:igfitidea点击:

create a web-service request xml from xsd

javaxmlweb-servicesxsdwsdl

提问by S4beR

I want to consume a web-service without using any framework like jax-ws or axis. By checking this article, i know that I need to create request xml. Is there a way for me to parse the wsdl and create the request xml dynamically? I have checked XSInstancefor xsd but not sure how to use it with wsdls

note: a web-service may have multiple operations and i need to create request xml for any of them based on some parameter

我想在不使用任何框架(如 jax-ws 或轴)的情况下使用 Web 服务。通过查看这篇文章,我知道我需要创建请求 xml。有没有办法让我解析 wsdl 并动态创建请求 xml ?我已经检查了 xsd 的XSInstance,但不确定如何将它与 wsdls 一起使用

注意:Web 服务可能有多个操作,我需要根据某个参数为其中任何一个创建请求 xml

采纳答案by Roman Vottner

Some frameworks aren't there for no reason - but if you want to go the route step by step, you should first have a look at the methods defined in the WSDL contract and add the parameters contained in the message part to the methods. To showcase the relation between a WSDL contract and a SOAP message I use an example WSDL contract taken from http://www.tutorialspoint.com/wsdl/wsdl_example.htmas the WSDL file in your link does not work for me

某些框架无缘无故不存在 - 但如果您想一步一步地走这条路线,您应该首先查看 WSDL 合同中定义的方法,并将消息部分中包含的参数添加到方法中。为了展示 WSDL 合同和 SOAP 消息之间的关系,我使用了来自http://www.tutorialspoint.com/wsdl/wsdl_example.htm的示例 WSDL 合同,因为您链接中的 WSDL 文件对我不起作用

<definitions name="HelloService"
   targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <message name="SayHelloRequest">
      <part name="firstName" type="xsd:string"/>
   </message>
   <message name="SayHelloResponse">
      <part name="greeting" type="xsd:string"/>
   </message>

   <portType name="Hello_PortType">
      <operation name="sayHello">
         <input message="tns:SayHelloRequest"/>
         <output message="tns:SayHelloResponse"/>
      </operation>
   </portType>

   <binding name="Hello_Binding" type="tns:Hello_PortType">
   <soap:binding style="rpc"
      transport="http://schemas.xmlsoap.org/soap/http"/>
   <operation name="sayHello">
      <soap:operation soapAction="sayHello"/>
      <input>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice"
            use="encoded"/>
      </input>
      <output>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice"
            use="encoded"/>
      </output>
   </operation>
   </binding>

   <service name="Hello_Service">
      <documentation>WSDL File for HelloService</documentation>
      <port binding="tns:Hello_Binding" name="Hello_Port">
         <soap:address
            location="http://www.examples.com/SayHello/">
      </port>
   </service>
</definitions>

A WSDL file contains the following parts: service, binding, portType, operations, messages.

WSDL 文件包含以下部分:服务、绑定、端口类型、操作、消息。

The service defines the actual web service which binds to a specified URL and offers the operations presented in the WSDL contract. The portType defines ports for incoming and outgoing messages and the message segment defines which parameters and return values to expect for messages received and sent. The binding itself can either be rpcor documentand here again encodedor literal- this setting will affect how the actual SOAP body will look like - more information: http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/

该服务定义了实际的 Web 服务,该服务绑定到指定的 URL 并提供 WSDL 合同中提供的操作。portType 定义传入和传出消息的端口,消息段定义接收和发送的消息需要哪些参数和返回值。绑定本身可以是rpcdocument和 再次encodedliteral- 此设置将影响实际 SOAP 主体的外观 - 更多信息:http: //www.ibm.com/developerworks/webservices/library/ws-whichwsdl/

Furthermore, a WSDL file can either contain a link to an xsd that defines the message-parameters or return types or contain the whole xsd definition within the contract.

此外,WSDL 文件可以包含指向定义消息参数或返回类型的 xsd 的链接,也可以包含合同中的整个 xsd 定义。

In Java the method declaration of the SayHelloRequest would look like this: public String sayHello(String firstName);but invoking a SOAP based service requires an XML (SOAP) message to be sent to a listening service like this:

在 Java 中,SayHelloRequest 的方法声明如下所示:public String sayHello(String firstName);但调用基于 SOAP 的服务需要将 XML (SOAP) 消息发送到如下所示的侦听服务:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"    
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <soapenv:Header/>
   <soapenv:Body>
       <SayHelloRequest>
           <firstName xsi:type="xsd:string">Test</firstName>
       </SayHelloRequest>
   </soapenv:Body>
</soapenv:Envelope>

So, building a SOAP message from WSDL without any framework is possible, but you have to deal with the overhead it brings to the table. Moreover you will have to validate the xsd on your own to be on the safe side.

因此,可以在没有任何框架的情况下从 WSDL 构建 SOAP 消息,但您必须处理它带来的开销。此外,为了安全起见,您必须自己验证 xsd。

With this knowledge you can write your own parser that first extracts the service part, then the binding and the port type (with the defined encoding) and last but not least the defined operations with the in- and out-messages for each operation. To learn which types those parameters are, you need to further have a look at the xsd types and find similar Java classes therefore.

有了这些知识,您可以编写自己的解析器,首先提取服务部分,然后是绑定和端口类型(具有定义的编码),最后但并非最不重要的是定义的操作以及每个操作的输入和输出消息。要了解这些参数是什么类型,您需要进一步查看 xsd 类型并因此找到类似的 Java 类。

HTH

HTH