webservice 之 WSDL的解析

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   strong   

 

先看一個wsdl, 

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:tns="http://ws.lk.com"    xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"    xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.lk.com">    <wsdl:types>        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"            attributeFormDefault="qualified" elementFormDefault="qualified"            targetNamespace="http://ws.lk.com">            <xsd:element name="example">                <xsd:complexType>                    <xsd:sequence>                        <xsd:element maxOccurs="1" minOccurs="1" name="in0"                            nillable="true" type="xsd:string" />                    </xsd:sequence>                </xsd:complexType>            </xsd:element>            <xsd:element name="exampleResponse">                <xsd:complexType>                    <xsd:sequence>                        <xsd:element maxOccurs="1" minOccurs="1" name="out"                            nillable="true" type="xsd:string" />                    </xsd:sequence>                </xsd:complexType>            </xsd:element>        </xsd:schema>    </wsdl:types>    <wsdl:message name="exampleRequest">        <wsdl:part name="parameters" element="tns:example"></wsdl:part>    </wsdl:message>    <wsdl:message name="exampleResponse">        <wsdl:part name="parameters" element="tns:exampleResponse"></wsdl:part>    </wsdl:message>    <wsdl:portType name="HelloWSsssPortType">        <wsdl:operation name="example">            <wsdl:input name="exampleRequest" message="tns:exampleRequest"></wsdl:input>            <wsdl:output name="exampleResponse" message="tns:exampleResponse"></wsdl:output>        </wsdl:operation>    </wsdl:portType>    <wsdl:binding name="HelloWSsssHttpBinding" type="tns:HelloWSsssPortType">        <wsdlsoap:binding style="document"            transport="http://schemas.xmlsoap.org/soap/http" />        <wsdl:operation name="example">            <wsdlsoap:operation soapAction="" />            <wsdl:input name="exampleRequest">                <wsdlsoap:body use="literal" />            </wsdl:input>            <wsdl:output name="exampleResponse">                <wsdlsoap:body use="literal" />            </wsdl:output>        </wsdl:operation>    </wsdl:binding>    <wsdl:service name="HelloWSsss">        <wsdl:port name="HelloWSsssHttpPort" binding="tns:HelloWSsssHttpBinding">            <wsdlsoap:address location="http://localhost:8080/HelloWS/services/HelloWSsss" />        </wsdl:port>    </wsdl:service></wsdl:definitions>

首先通過MyEclipse 的 New web service 嚮導產生java代碼,可以同時產生介面、實作類別,然後訪問http://localhost:8080/HelloWS/services/HelloWSsss?wsdl 得到的。

如果通過MyEclipse 的 MyEclipse WSDL Editor開啟,則得到:

 

類似這樣的圖片,看到個很多次了,每次都是看不懂。。。 鬱悶。 不過,其實要是稍微研究對應的.wsdl一下,則發現,它沒那麼的嚇人。

基本結構就是:

wsdl:service  有wsdl:port 的子項目,wsdl:port 有子項目wsdlsoap:address 提供對外訪問的url。 wsdl:port 同時由binding屬性綁定了一個wsdl:binding 。

wsdl:binding 有type屬性指定了wsdl:portType  ,wsdl:portType  包含子項目wsdl:operation ,wsdl:operation 的name屬性指定了其對外提供的方法。

子項目wsdl:input 、wsdl:output  分部對應方法的參數、傳回值。wsdl:input 、wsdl:output 的message屬性指定了wsdl:message ,子項目wsdl:part 的element屬性指定了

包含在wsdl:types中的xsd:element ,xsd:element 子子項目的type屬性指定了 具體的資料類型。

 

這樣的描述顯得混亂,但是我不太會通過繪圖來分析。先這樣吧。

 

規律:

如果是通過New web service 嚮導產生的, 預設的各元素屬性名稱字,非常有規律:

假設 ws 名字為 

HelloWSsss ,那麼 ——> binding="tns:HelloWSsssHttpBinding" -> type="tns:HelloWSsssPortType" -> wsdl:operation name="example"
兵分兩路:
-> wsdl:input name="exampleRequest" -> element="tns:example"
-> wsdl:output name="exampleResponse" -> element="tns:exampleResponse"

__ 就這樣,注意到, input參數message名 為 exampleRequest ,到了element標籤的時候,名字重新變回了 example, 當然,這個無關緊要,非常小的細節。。。

 

xsd:element -> wsdl:message  -> wsdl:portType  : wsdl:operation  -> input 、 output 兩個 wsdl:message  -> 兩個 xsd:element 

 

 

不搞了,反正已經完全不懼怕wsdl了!!

 

看到那樣的圖,也完全不用擔心看不懂了! 其實很簡單,只是wsdl 這樣的格式把一個非常簡單的ws描述的複雜起來,當然,這也可能是為了能夠處理複雜問題,而逐漸完善起來的規範。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.