![](/img/trans.png)
[英]How to send a request (and receive it) in XML format from some web-service?
[英]create a web-service request xml from xsd
我想在不使用jax-ws或axis之類的框架的情況下使用Web服務。 通過檢查本文 ,我知道我需要創建請求xml 。 有沒有辦法讓我解析wsdl並動態創建請求xml? 我已經檢查了XSInstance的xsd,但不確定如何與wsdls一起使用
注意 :Web服務可能有多個操作,我需要根據某個參數為其中任何一個創建請求xml
某些框架不是無緣無故存在的-但是,如果您想逐步進行開發,則應該首先查看WSDL合同中定義的方法,並將消息部分中包含的參數添加到方法中。 為了展示WSDL合同和SOAP消息之間的關系,我使用了一個示例WSDL合同,該合同摘自http://www.tutorialspoint.com/wsdl/wsdl_example.htm,因為您鏈接中的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>
WSDL文件包含以下部分:服務,綁定,portType,操作,消息。
該服務定義綁定到指定URL並提供WSDL合同中提供的操作的實際Web服務。 portType定義了傳入和傳出消息的端口,消息段定義了期望接收和發送消息的參數和返回值。 綁定本身可以是rpc
或document
,這里又可以是encoded
或literal
-此設置將影響實際SOAP主體的外觀-更多信息: http : //www.ibm.com/developerworks/webservices/library/ws- whichwsdl /
而且,WSDL文件可以包含指向定義消息參數或返回類型的xsd的鏈接,也可以包含協定中的整個xsd定義。
在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>
因此,可以在沒有任何框架的情況下從WSDL構建SOAP消息,但是您必須處理它給表帶來的開銷。 此外,為了安全起見,您必須自己驗證xsd。
有了這些知識,您可以編寫自己的解析器,該解析器首先提取服務部分,然后提取綁定和端口類型(具有定義的編碼),最后但並非最不重要的是提取定義的操作,其中每個操作都帶有輸入和輸出消息。 要了解這些參數是哪種類型,您需要進一步查看xsd類型並因此找到類似的Java類。
高溫超導
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.