WebService学习总结(3):如何查看WSDL文档

  作为webservice客户端开发,在日常工作中可能经常会拿到一个对方提供的wsdl地址或文档,那么拿到这个地址后我们如何编写客户端调用代码呢,前面几篇只是以个人经验的方式写了下,那么真正要根据wsdl文档来编写客户端调用代码就必须学会看懂wsdl文档。下面就结合之前基础篇的demo来深入剖析下wsdl文档,最后以图解的方式形象说明下。

   本文以之前基础篇的SayHello的Demo来深入分析下wsdl文档的几个部分,个人认为可以共分6部分,下面分别介绍:

<definitions/>

        这部分在基础篇里已经介绍,主要说明引用了哪些schema以及schema的位置等,可以看下基础篇的介绍,SayHello的Demo这部分内容如下:

  1. <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  2.     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.server.ws.devins.com/"  
  3.     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"  
  4.     xmlns:ns1="http://service.server.ws.devins.com/" name="SayHelloImplService"  
  5.     targetNamespace="http://impl.service.server.ws.devins.com/">  

<types/>

  1.          <!--   
  2.          types  
  3.          schema:约束xml格式  
  4.          element:用来指定xml中的标签  
  5.                  <sayHello></sayhello>  
  6.                  <sayHelloResponse></sayHelloResponse>  
  7.          complexType:说明是一个复合类型  
  8.                           请求   
  9.                  <sayHello>  
  10.                     <arg0>string</arg0>  
  11.                  </sayhello>  
  12.                    响应  
  13.                  <sayHelloResponse>  
  14.                     <return>string</return>  
  15.                  </sayHelloResponse>  
  16.                    
  17.         回看下demo的请求与响应的核心内容  
  18.             <q0:sayHello>  
  19.               <arg0>devins</arg0>   
  20.             </q0:sayHello>  
  21.               
  22.             <ns2:sayHelloResponse">  
  23.               <return>Hello: devins</return>   
  24.             </ns2:sayHelloResponse>  
  25.                     
  26.       -->  
  27.     <wsdl:types>  
  28.         <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  29.             xmlns:tns="http://service.server.ws.devins.com/" elementFormDefault="unqualified"  
  30.             targetNamespace="http://service.server.ws.devins.com/" version="1.0">  
  31.             <xs:element name="sayHello" type="tns:sayHello" />  
  32.             <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />  
  33.             <xs:complexType name="sayHello">  
  34.                 <xs:sequence>  
  35.                     <xs:element minOccurs="0" name="arg0" type="xs:string" />  
  36.                 </xs:sequence>  
  37.             </xs:complexType>  
  38.             <xs:complexType name="sayHelloResponse">  
  39.                 <xs:sequence>  
  40.                     <xs:element minOccurs="0" name="return" type="xs:string" />  
  41.                 </xs:sequence>  
  42.             </xs:complexType>  
  43.         </xs:schema>  
  44.     </wsdl:types>  

<message/>

  1.         <!--   
  2.         message:用来定义soap消息结构  
  3.         part:部分/组成的意思  
  4.         实际上引用的就是上面schema中的约束格式  
  5.      -->  
  6.     <wsdl:message name="sayHelloResponse">  
  7.         <wsdl:part element="ns1:sayHelloResponse" name="parameters" />  
  8.     </wsdl:message>  
  9.     <wsdl:message name="sayHello">  
  10.         <wsdl:part element="ns1:sayHello" name="parameters" />  
  11.     </wsdl:message>  

<portType/>

  1.         <!--   
  2.         portType:用来指定服务器端的SEI(接口)  
  3.         operation:表示操作/行为,即SEI中定义的方法  
  4.         input:方法sayHello的输入  
  5.         output:方法sayHello的输出  
  6.         输入输出引用的是上面message的定义  
  7.      -->  
  8.     <wsdl:portType name="ISayHello">  
  9.         <wsdl:operation name="sayHello">  
  10.             <wsdl:input message="ns1:sayHello" name="sayHello" />  
  11.             <wsdl:output message="ns1:sayHelloResponse" name="sayHelloResponse" />  
  12.         </wsdl:operation>  
  13.     </wsdl:portType>  

<binding/>

  1. <!--   
  2.     binding:用来指定SEI的实现类  
  3.     type属性:引用<portType>定义  
  4.     <soap:binding style="document">:表示传输的一个document (xml)  
  5.     <input><output>与上节说的相同  
  6.     <soap:body use="literal" />:表示body传输采用文本即xml格式的文本  
  7.  -->  
  8. <wsdl:binding name="SayHelloImplServiceSoapBinding" type="ns1:ISayHello">  
  9.     <soap:binding style="document"  
  10.         transport="http://schemas.xmlsoap.org/soap/http" />  
  11.     <wsdl:operation name="sayHello">  
  12.         <soap:operation soapAction="" style="document" />  
  13.         <wsdl:input name="sayHello">  
  14.             <soap:body use="literal" />  
  15.         </wsdl:input>  
  16.         <wsdl:output name="sayHelloResponse">  
  17.             <soap:body use="literal" />  
  18.         </wsdl:output>  
  19.     </wsdl:operation>  
  20. </wsdl:binding>  

<service>


  1. <!--   
  2.     service:相同于webservice容器,也可理解为一个工厂  
  3.     name:用于指定客户端的容器类/工厂类,客户端代码从此类开始  
  4.     port:用来指定服务器端的一个入口(对应SEI的实现类)  
  5.     port binding:引用上面定义的  
  6.     port name:容器通过这个方法获得实现类  
  7.     address:客户端真正用于请求的地址  
  8.       
  9.     回想我们的demo:  
  10.     SayHelloImplService factory = new SayHelloImplService();  
  11.     SayHelloImpl sayHelloImpl = factory.getSayHelloImplPort();  
  12.  -->  
  13. <wsdl:service name="SayHelloImplService">  
  14.     <wsdl:port binding="tns:SayHelloImplServiceSoapBinding"  
  15.         name="SayHelloImplPort">  
  16.         <soap:address location="http://132.122.239.74:8089/ws/sayhello" />  
  17.     </wsdl:port>  
  18. </wsdl:service>  

        以上就webservice基础篇里的SayHello为例,详细对wsdl进行了剖析,最后再结合上面的分析以图示的方式形象展现下,希望对需要深入理解wsdl文档的朋友有一定帮助。


图解WSDL

根据WSDL文档中的几部分,以及各部分之间的引用关系,下面以图例的方式展现如下:

说明:上图中的箭头表示引用关系。