WSDL结构

1. WSDL概述

  WSDL(Web服务描述语言,Web Services Description Language)是为描述Web服务发布的XML格式。WSDL描述Web服务的公共接口。这是一个基于XML的关于如何与Web服务通讯和使用的服务描述;也就是描述与目录中列出的Web服务进行交互时需要绑定的协议和信息格式。

2. WSDL总体结构

  wsdl都是由definition、types、message、protType、bingding、service这6个大的节点组成,各个节点之间的关系参考图2。

  

  

 3. WSDL结构详细介绍

  我们以如下WSDL文件骤行解析

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
 3              xmlns:wsp="http://www.w3.org/ns/ws-policy"
 4              xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
 5              xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
 6              xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 7              xmlns:tns="http://impl.service.webservice.jdk.suns.com/"
 8              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 9              xmlns="http://schemas.xmlsoap.org/wsdl/"
10              targetNamespace="http://impl.service.webservice.jdk.suns.com/" name="FlightServiceImplService">
11     <types>
12         <xsd:schema xmlns:tns="http://impl.service.webservice.jdk.suns.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
13                     version="1.0" targetNamespace="http://impl.service.webservice.jdk.suns.com/">
14 
15             <xs:element name="getFlightInfo" type="tns:getFlightInfo"></xs:element>
16 
17             <xs:element name="getFlightInfoResponse" type="tns:getFlightInfoResponse"></xs:element>
18 
19             <xs:complexType name="getFlightInfo">
20                 <xs:sequence>
21                     <xs:element name="arg0" type="xs:string" minOccurs="0"></xs:element>
22                     <xs:element name="arg1" type="xs:dateTime" minOccurs="0"></xs:element>
23                 </xs:sequence>
24             </xs:complexType>
25 
26             <xs:complexType name="getFlightInfoResponse">
27                 <xs:sequence>
28                     <xs:element name="return" type="tns:flightInfo" minOccurs="0" maxOccurs="unbounded"></xs:element>
29                 </xs:sequence>
30             </xs:complexType>
31 
32             <xs:complexType name="flightInfo">
33                 <xs:sequence>
34                     <xs:element name="airlineICAO" type="xs:string" minOccurs="0"></xs:element>
35                     <xs:element name="fligtDate" type="xs:dateTime" minOccurs="0"></xs:element>
36                     <xs:element name="fligtDirectCd" type="xs:string" minOccurs="0"></xs:element>
37                     <xs:element name="fligtNo" type="xs:string" minOccurs="0"></xs:element>
38                 </xs:sequence>
39             </xs:complexType>
40         </xsd:schema>
41     </types>
42     <message name="getFlightInfo">
43         <part name="parameters" element="tns:getFlightInfo"></part>
44     </message>
45     <message name="getFlightInfoResponse">
46         <part name="parameters" element="tns:getFlightInfoResponse"></part>
47     </message>
48     <portType name="FlightServiceImpl">
49         <operation name="getFlightInfo">
50             <input wsam:Action="http://impl.service.webservice.jdk.suns.com/FlightServiceImpl/getFlightInfoRequest"
51                    message="tns:getFlightInfo"></input>
52             <output wsam:Action="http://impl.service.webservice.jdk.suns.com/FlightServiceImpl/getFlightInfoResponse"
53                     message="tns:getFlightInfoResponse"></output>
54         </operation>
55     </portType>
56     <binding name="FlightServiceImplPortBinding" type="tns:FlightServiceImpl">
57         <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
58         <operation name="getFlightInfo">
59             <soap:operation soapAction=""></soap:operation>
60             <input>
61                 <soap:body use="literal"></soap:body>
62             </input>
63             <output>
64                 <soap:body use="literal"></soap:body>
65             </output>
66         </operation>
67     </binding>
68     <service name="FlightServiceImplService">
69         <port name="FlightServiceImplPort" binding="tns:FlightServiceImplPortBinding">
70             <soap:address location="http://127.0.0.1:8080/webService/flightService"></soap:address>
71         </port>
72     </service>
73 </definitions>
  3.1 第1行:
<?xml version="1.0" encoding="UTF-8"?>

   version="1.0" 声明用的xml版本是1.0

    encoding="UTF-8" 声明用xml传输数据的时候的字符编码,假如文档里面有中文,编码方式不是UTF-8,传输过去再解码的话中文就会是乱码

  3.2 2-10行:definitions节点
1 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
2              xmlns:wsp="http://www.w3.org/ns/ws-policy"
3              xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
4              xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
5              xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
6              xmlns:tns="http://impl.service.webservice.jdk.suns.com/"
7              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
8              xmlns="http://schemas.xmlsoap.org/wsdl/"
9              targetNamespace="http://impl.service.webservice.jdk.suns.com/" name="FlightServiceImplService">
  • targetNamespace属性值来定义当前schema的命名空间,所谓命名空间可以理解为唯一识别id,它的值经常会设置成一个url(这个url地址不一定能访问)
  • 通过xmlns属性来引入其他schema,xmlns值就是其他schema的targetNameSpace。比如当前schema命名空间为http://impl.service.webservice.jdk.suns.com/,通过xmlns:tns来引入自身,并取了别名为tns
  • 别名:而xmlns后面是命名空间的别名,使用别名的作用是防止不同schema中有相同的element。比如有个elementTypeTest节点同时存在wsu、wsp命名空间,我们可以通过wsu:elementTypeTest来指定使用wsu命名空间中的elementTypeTest
   3.3 11-41行:types节点
 1     <types>
 2         <xsd:schema xmlns:tns="http://impl.service.webservice.jdk.suns.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
 3                     version="1.0" targetNamespace="http://impl.service.webservice.jdk.suns.com/">
 4 
 5             <xs:element name="getFlightInfo" type="tns:getFlightInfo"></xs:element>
 6 
 7             <xs:element name="getFlightInfoResponse" type="tns:getFlightInfoResponse"></xs:element>
 8 
 9             <xs:complexType name="getFlightInfo">
10                 <xs:sequence>
11                     <xs:element name="arg0" type="xs:string" minOccurs="0"></xs:element>
12                     <xs:element name="arg1" type="xs:dateTime" minOccurs="0"></xs:element>
13                 </xs:sequence>
14             </xs:complexType>
15 
16             <xs:complexType name="getFlightInfoResponse">
17                 <xs:sequence>
18                     <xs:element name="return" type="tns:flightInfo" minOccurs="0" maxOccurs="unbounded"></xs:element>
19                 </xs:sequence>
20             </xs:complexType>
21 
22             <xs:complexType name="flightInfo">
23                 <xs:sequence>
24                     <xs:element name="airlineICAO" type="xs:string" minOccurs="0"></xs:element>
25                     <xs:element name="fligtDate" type="xs:dateTime" minOccurs="0"></xs:element>
26                     <xs:element name="fligtDirectCd" type="xs:string" minOccurs="0"></xs:element>
27                     <xs:element name="fligtNo" type="xs:string" minOccurs="0"></xs:element>
28                 </xs:sequence>
29             </xs:complexType>
30         </xsd:schema>
31     </types>

     Types元素用作一个容器,定义了自定义的特殊数据结构。在Types的schema中定义了2个复合元素:getFlightInfo和getFlightInfoResponse

      getFlightInfo元素:第5行getFlightInfo元素的数据类型通过type属性指定,而type属性值指向了schema中第9行getFlightInfo复合类型,该复合类型有2个String类型元素arg0、arg1,最终可以知道第5行getFlightInfo元素的数据结构为如下:

<getFlightInfo>
    <arg0>String<arg0>
    <arg1>String<arg1>
</getFlightInfo>

       getFlightInfoResponse元素:第7行getFlightInfoResponse元素的数据类型通过type属性指定,而type属性值指向了schema中第16行getFlightInfoResponse复合类型,该复合类型有个元素return,return元素的类型通过type属性指向第22行的flightInfo复合类型,flightInfo有4个String类型的元素,分别为airlineICAO、flightDate、flightDirectCd、flightNo,最终可以知道第7行getFlightInfoResponse元素的数据结构如下:

<getFlightInfoResponse>
    <return>
        <airlineICAO>String</airlineICAO>
        <flightDate>String</flightDate>
        <flightDirectCd></flightDirectCd>
        <flightNO>String</flightNO>
    </return>
</getFlightInfoResponse>
  3.4 import节点

   import元素使得可以在当前的WSDL文档中使用其他WSDL文档中指定的命名空间中的定义元素。本例子中没有使用import元素。通常在用户希望模块化WSDL文档的时候,该功能是非常有效果的。 
import的格式如下:

<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/>

必须有namespace属性和location属性: 

  • namespace属性:值必须与正导入的WSDL文档中声明的targetNamespace相匹配; 
  • location属性:必须指向一个实际的WSDL文档,并且该文档不能为空。
  3.5 42-47行:message节点
1     <message name="getFlightInfo">
2         <part name="parameters" element="tns:getFlightInfo"></part>
3     </message>
4     <message name="getFlightInfoResponse">
5         <part name="parameters" element="tns:getFlightInfoResponse"></part>
6     </message>

    message表示web service输入消息和返回消息,其中part节点引用types中定义的数据结构,这里message:getFlightInfo就是types中getFlightInfo数据结构。

  3.6 48-55行:portType节点
1 <portType name="FlightServiceImpl">
2         <operation name="getFlightInfo">
3             <input wsam:Action="http://impl.service.webservice.jdk.suns.com/FlightServiceImpl/getFlightInfoRequest"
4                    message="tns:getFlightInfo"></input>
5             <output wsam:Action="http://impl.service.webservice.jdk.suns.com/FlightServiceImpl/getFlightInfoResponse"
6                     message="tns:getFlightInfoResponse"></output>
7         </operation>
8     </portType>

    portType元素定义了Web服务的抽象接口。该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来说明Web服务实现使用的Internet协议、编码方案以及Internet地址。 

    一个portType中可以定义多个operation,一个operation可以看作是服务端处理请求的方法

      input: 指定客户端传给服务端的数据,通过message属性来引用上面定义<message>

      output: 指定服务器端返回给客户端的数据, 通过message属性来引用上面定义<message>

  3.6 56-67行:binding节点
 1 <binding name="FlightServiceImplPortBinding" type="tns:FlightServiceImpl">
 2         <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
 3         <operation name="getFlightInfo">
 4             <soap:operation soapAction=""></soap:operation>
 5             <input>
 6                 <soap:body use="literal"></soap:body>
 7             </input>
 8             <output>
 9                 <soap:body use="literal"></soap:body>
10             </output>
11         </operation>
12     </binding>

    portType元素定义了Web服务的抽象接口,binding则为Web服务的具体实现。binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。

      type属性:引用上面的<portType>

      soap:binding:元素指定了用于传输SOAP消息的Internet协议(这里是http)以及operation缺省的消息类型(这里是document,其实就是XML)

      operation:用来定义实现的方法,也就是后台具体处理客户端请求的方法。

        input: 指定客户端应用传过来的数据,literal表示文本数据

        output: 指定服务器端返回给客户端的数据, literal表示文本数据

  3.7 68-72行:service节点
1     <service name="FlightServiceImplService">
2         <port name="FlightServiceImplPort" binding="tns:FlightServiceImplPortBinding">
3             <soap:address location="http://127.0.0.1:8080/webService/flightService"></soap:address>
4         </port>
5     </service>

    service相关服务访问点的集合,集合中包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。

      第1行name属性:在封闭的WSDL文档中提供唯一名称

      port:用来指定一个服务器端处理请求的入口

        binding属性:引用上面定义的<bingding>标签

        address:当前webservice的请求地址

 

  

原文地址:https://www.cnblogs.com/kesuns/p/12566597.html