1、逐个分析wsdl文件中的元素:
<types>:数据类型定义的容器,一般使用 xml schema类型系统。
<message>:通信消息的数据结构的抽象化定义,使用<types>定义的数据类型来定义整个消息的数据结构。(portType相当于一个类或者接口)
<operation>:对服务中所支持的操作的抽象描述,一般单个<operation>描述了一个访问入口的请求/响应消息对。
<portType>:服务访问点所支持的操作的抽象集合,相当于一个类。
<binding>:特定端口类型(portType)具体协议和数据格式规范的绑定(绑定这里是个名词)。
<port>: 具体的绑定(binding)与web地址组合的单个访问点。
<service>:相关服务访问点(port)的集合。
2、关系图(此图非原创)
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规范。一般可以通用。
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:变量名="协议源",定义的这个变量名,可以在后面定义具体参数类型时用到。
<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,就相当于在写一个普通类时定义变量或者对象引用,此处类似于对象引用。
此处这两个元素都是复杂类型,sequence定义该复杂类型里面的属性变量。
<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表示输入参数、output表示输出参数。
这里刚开始理解起来比较绕,使用wsdl的有两类人:客户端、服务端。
写好wsdl后,需要使用工具自动生成Java代码(一般通过cxf命令生成),客户端和服务端的代码稍有区别:
服务端: public sayHelloResponse sayHelloRequest(sayHelloRequest parameters)//
客户端: public sayHelloResquest sayHelloRequest(sayHelloResponse parameters)//
<binding>
binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。通常binding元素与协议专有的元素和在一起使用,本文中的例子:
<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服务。
port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。
文档中的例子:
<wsdl:service name="HelloService"> <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding"> <wsdlsoap:address location="http://localhost:8080/xfire/services/HelloService" /> </wsdl:port> </wsdl:service>
这部分是具体的Web服务的定义,在这个名为HelloService的Web服务中,提供了一个服务访问入口,
访问地址是http://localhost:8080/xfire/services/HelloService,使用的消息模式是由前面的binding所定义的。
以后慢慢完善之。。。