wsdl透明解析
1、逐個分析wsdl文件中的元素:
? ? ?<types>:數(shù)據(jù)類型定義的容器,一般使用 xml schema類型系統(tǒng)。
? ? ?<message>:通信消息的數(shù)據(jù)結(jié)構(gòu)的抽象化定義,使用<types>定義的數(shù)據(jù)類型來定義整個消息的數(shù)據(jù)結(jié)構(gòu)。(portType相當(dāng)于一個類或者接口)
? ? ?<operation>:對服務(wù)中所支持的操作的抽象描述,一般單個<operation>描述了一個訪問入口的請求/響應(yīng)消息對。
? ? ?<portType>:服務(wù)訪問點所支持的操作的抽象集合,相當(dāng)于一個類。
? ? ?<binding>:特定端口類型(portType)具體協(xié)議和數(shù)據(jù)格式規(guī)范的綁定(綁定這里是個名詞)。
? ? ?<port>: ?具體的綁定(binding)與web地址組合的單個訪問點。
? ? ?<service>:相關(guān)服務(wù)訪問點(port)的集合。
2、關(guān)系圖(此圖非原創(chuàng)) ? ? ? ? ? ? ? ? ?
3、例子講解
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <wsdl:definitions 3 targetNamespace="http://com.liuxiang.xfireDemo/HelloService" 4 xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" 5 xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 6 xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" 7 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 8 xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" 9 xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" 10 xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" 11 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 12 <wsdl:types> 13 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 14 attributeFormDefault="qualified" elementFormDefault="qualified" 15 targetNamespace="http://com.liuxiang.xfireDemo/HelloService"> 16 <xsd:element name="sayHellorequest"> 17 <xsd:complexType> 18 <xsd:sequence> 19 <xsd:element maxOccurs="1" minOccurs="1" 20 name="name" nillable="true" type="xsd:string" /> 21 </xsd:sequence> 22 </xsd:complexType> 23 </xsd:element> 24 <xsd:element name="sayHelloResponse"> 25 <xsd:complexType> 26 <xsd:sequence> 27 <xsd:element maxOccurs="1" minOccurs="1" 28 name="out" nillable="true" type="xsd:string" /> 29 </xsd:sequence> 30 </xsd:complexType> 31 </xsd:element> 32 </xsd:schema> 33 </wsdl:types> 34 <wsdl:message name="sayHelloResponse"> 35 <wsdl:part name="parameters" element="tns:sayHelloResponse" /> 36 </wsdl:message> 37 <wsdl:message name="sayHelloRequest"> 38 <wsdl:part name="parameters" element="tns:sayHelloRequest" /> 39 </wsdl:message> 40 <wsdl:portType name="HelloServicePortType"> 41 <wsdl:operation name="sayHellorequest"> 42 <wsdl:input name="sayHelloRequest" 43 message="tns:sayHelloRequest" /> 44 <wsdl:output name="sayHelloResponse" 45 message="tns:sayHelloResponse" /> 46 </wsdl:operation> 47 </wsdl:portType> 48 <wsdl:binding name="HelloServiceHttpBinding" 49 type="tns:HelloServicePortType"> 50 <wsdlsoap:binding style="document" 51 transport="http://schemas.xmlsoap.org/soap/http" /> 52 <wsdl:operation name="sayHello"> 53 <wsdlsoap:operation soapAction="" /> 54 <wsdl:input name="sayHelloRequest"> 55 <wsdlsoap:body use="literal" /> 56 </wsdl:input> 57 <wsdl:output name="sayHelloResponse"> 58 <wsdlsoap:body use="literal" /> 59 </wsdl:output> 60 </wsdl:operation> 61 </wsdl:binding> 62 <wsdl:service name="HelloService"> 63 <wsdl:port name="HelloServiceHttpPort" 64 binding="tns:HelloServiceHttpBinding"> 65 <wsdlsoap:address 66 location="http://localhost:8080/xfire/services/HelloService" /> 67 </wsdl:port> 68 </wsdl:service> 69 </wsdl:definitions>4、逐部分介紹
? ? ?<definition>
1 <wsdl:definitions 2 targetNamespace="http://com.liuxiang.xfireDemo/HelloService" 3 xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" 4 xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 5 xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" 6 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 7 xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" 8 xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" 9 xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" 10 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 11 </wsdl:definitions>主要是提供一個命名空間、整個wsdl文檔中用到的xml規(guī)范。一般可以通用。
xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:變量名="協(xié)議源",定義的這個變量名,可以在后面定義具體參數(shù)類型時用到。
? ? ?<type>
<wsdl:types><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"attributeFormDefault="qualified" elementFormDefault="qualified"targetNamespace="http://com.liuxiang.xfireDemo/HelloService"><xsd:element name="sayHelloRequest"><xsd:complexType><xsd:sequence><xsd:element maxOccurs="1" minOccurs="1"name="name" nillable="true" type="xsd:string" /></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="sayHelloResponse"><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>這部分可以單獨定義為一個文件,因為有時候需要定義的元素會比較多,單獨寫一個文件,用import引入到需要用的wsdl文件。
這里定義了兩個元素: sayHelloRequest/sayHelloResponse,就相當(dāng)于在寫一個普通類時定義變量或者對象引用,此處類似于對象引用。
此處這兩個元素都是復(fù)雜類型,sequence定義該復(fù)雜類型里面的屬性變量。
<message>
<wsdl:message name="sayHelloResponse"><wsdl:part name="parameters" element="tns:sayHelloResponse" /></wsdl:message><wsdl:message name="sayHelloRequest"><wsdl:part name="parameters" element="tns:sayHelloRequest" /></wsdl:message>消息格式的抽象定義。
<portType>
<wsdl:portType name="HelloServicePortType"><wsdl:operation name="sayHelloRequest"><wsdl:input name="sayHelloRequest"message="tns:sayHelloRequest" /><wsdl:output name="sayHelloResponse"message="tns:sayHelloResponse" /></wsdl:operation></wsdl:portType><portType>定義了抽象接口,<operation>定義了接口里面的方法,input表示輸入?yún)?shù)、output表示輸出參數(shù)。
這里剛開始理解起來比較繞,使用wsdl的有兩類人:客戶端、服務(wù)端。
寫好wsdl后,需要使用工具自動生成Java代碼(一般通過cxf命令生成),客戶端和服務(wù)端的代碼稍有區(qū)別:
服務(wù)端: public sayHelloResponse sayHelloRequest(sayHelloRequest parameters)//
客戶端: public sayHelloResquest sayHelloRequest(sayHelloResponse parameters)// ? ? ?
<binding>
binding元素將一個抽象portType映射到一組具體協(xié)議(SOAO和HTTP)、消息傳遞樣式、編碼樣式。通常binding元素與協(xié)議專有的元素和在一起使用,本文中的例子:
<wsdl:binding name="HelloServiceHttpBinding"type="tns:HelloServicePortType"><wsdlsoap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="sayHello"><wsdlsoap:operation soapAction="" /><wsdl:input name="sayHelloRequest"><wsdlsoap:body use="literal" /></wsdl:input><wsdl:output name="sayHelloResponse"><wsdlsoap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding>service元素和port元素
service元素包含一個或者多個port元素,其中每個port元素表示一個不同的Web服務(wù)。
port元素將URL賦給一個特定的binding,甚至可以使兩個或者多個port元素將不同的URL賦值給相同的binding。
文檔中的例子:
<wsdl:service name="HelloService"><wsdl:port name="HelloServiceHttpPort"binding="tns:HelloServiceHttpBinding"><wsdlsoap:addresslocation="http://localhost:8080/xfire/services/HelloService" /></wsdl:port></wsdl:service>這部分是具體的Web服務(wù)的定義,在這個名為HelloService的Web服務(wù)中,提供了一個服務(wù)訪問入口,
訪問地址是http://localhost:8080/xfire/services/HelloService,使用的消息模式是由前面的binding所定義的。
以后慢慢完善之。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/dftencent/p/4031089.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
- 上一篇: 个人阅读 代码大全的阅读与提问
- 下一篇: 46 关于Linux的I/O重定向