WSDL 文档解析

      学习webservice,就离不了WSDL文档,他是我们开发WebService的基础,虽说,现在现在有许多WebService的开源框架使得我们可以根据WSDL生成客户端代码,但是,了解WSDL文档的结构还是有必要的。闲话不多说,我们开始进入正题。

      首先,我们看一份WSDL文档。

      

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server/" name="HelloWorldImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://server/" schemaLocation="http://localhost:8989/HelloWorld?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="HelloWorldImpl">
<operation name="sayHello">
<input wsam:Action="http://server/HelloWorldImpl/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://server/HelloWorldImpl/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloWorldImplPortBinding" type="tns:HelloWorldImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8989/HelloWorld"/>
</port>
</service>
</definitions>

这份文档就是我的上一篇文章,自己开发WebService的那个WSDL文档,咱们今天就以他为例进行讲解。

首先,对于一份WSDL文档,无论他多么复杂,都是由下面5个元素组成。

<types>  <message>  <portType>  <bingding>  <service>

首先,说一下,这个 <types> 它里面定义的是标签结构,在上述WSDL文档中的schema节点中的内容是

<xsd:import namespace="http://server/" schemaLocation="http://localhost:8989/HelloWorld?xsd=1"/>


 就是说他有导入了一个xsd文件,我们可以在浏览器中访问这个schemaLocation,会出现下面的内容

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
-->
<xs:schema xmlns:tns="http://server/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://server/">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

首先他有一个元素叫做sayHello,他的结构应该是这样的

<sayHello>

     <arg0>string</arg0>

</sayHello>

同样,元素 sayHelloResponse,他的结构应该是

<sayHelloResponse>

       <return>string</return>

</sayHelloResponse>

在<types>中就是定义了上面的文件格式。

<message>  用来定义消息的结构 soap消息 ,part : 指定引用types中定义的标签片断 

<portType>

portType: 用来定义服务器端的SEI 
operation : 用来指定SEI中的处理请求的方法 
input : 指定客户端应用传过来的数据, 会引用上面的定义的<message> 
output : 指定服务器端返回给客户端的数据, 会引用上面的定义的<message> 

<bingding>

binding : 用于定义SEI的实现类 
type属性: 引用上面的<portType> 
<soap:binding style="document"> : 绑定的数据是一个document(xml) 
operation : 用来定义实现的方法 
<soap:operation style="document" /> 传输的是document(xml) 
input: 指定客户端应用传过来的数据 
<soap:body use="literal" /> : 文本数据 
output : 指定服务器端返回给客户端的数据 
<soap:body use="literal" /> : 文本数据 

<service>

service : 一个webservice的容器 

name属性: 它用一指定客户端容器类 
port : 用来指定一个服务器端处理请求的入口(就SEI的实现) 
binding属性: 引用上面定义的<binding> 
address : 当前webservice的请求地址 

     明白了WSDL文档中的元素对应关系,我们就可以这么说,portType里写的是服务器端的“干”  ,bingding是客户端的“肉”,service 是帮助客户端来找到这个拥有服务端“干”的“肉”。



原文地址:https://www.cnblogs.com/linxiaoyang/p/4167087.html