700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > wsdl 与 soap协议详解

wsdl 与 soap协议详解

时间:2018-11-16 20:36:04

相关推荐

wsdl 与 soap协议详解

一、WSDL语言:(web service definition language - web service定义语言)

(一)简介:

1、wsdl 是全完基于xml 的,特别是xml schema。详见:XML学习笔记(三):XML规范:Schema详解。

2、wsdl 文档描述了 ws 主要的3个方面:

1)WHATA:该 ws 包含”什么“操作,即有几个方法。

2)HOW:该 ws 的操作应该”怎样“调用?

3)WHERE:该 ws 的服务地址。

3、详细的 wsdl 文件如下,给予Web Service笔记(二):利用CXF开发Web Service的服务和客户端。

1)实现类的 wsdl 地址为:http://localhost:8080/helloWorld?wsdl。内容如下:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<wsdl:definitions xmlns:xsd="/2001/XMLSchema" xmlns:wsdl="/wsdl/" xmlns:tns="/" xmlns:soap="/wsdl/soap/" xmlns:ns2="/soap/http" xmlns:ns1="/" name="HelloSAM" targetNamespace="/"><wsdl:import location="http://localhost:8080/helloWorld?wsdl=HelloWorld.wsdl" namespace="/"></wsdl:import><wsdl:binding name="HelloSAMSoapBinding" type="ns1:HelloWorld"><soap:binding style="document" transport="/soap/http"/><wsdl:operation name="sayHi"><soap:operation soapAction="" style="document"/><wsdl:input name="sayHi"><soap:body use="literal"/></wsdl:input><wsdl:output name="sayHiResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getCatsByUser"><soap:operation soapAction="" style="document"/><wsdl:input name="getCatsByUser"><soap:body use="literal"/></wsdl:input><wsdl:output name="getCatsByUserResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HelloSAM"><wsdl:port binding="tns:HelloSAMSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:8080/helloWorld"/></wsdl:port></wsdl:service></wsdl:definitions>

2)接口的 wsdl 地址为: http://localhost:8080/helloWorld?wsdl=HelloWorld.wsdl 。内容如下:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<wsdl:definitions xmlns:xsd="/2001/XMLSchema" xmlns:wsdl="/wsdl/" xmlns:ns1="/" name="HelloWorld" targetNamespace="/"><wsdl:types><xs:schema xmlns:xs="/2001/XMLSchema" xmlns:tns="/" elementFormDefault="unqualified" targetNamespace="/" version="1.0"><xs:element name="getCatsByUser" type="tns:getCatsByUser"/><xs:element name="getCatsByUserResponse" type="tns:getCatsByUserResponse"/><xs:element name="sayHi" type="tns:sayHi"/><xs:element name="sayHiResponse" type="tns:sayHiResponse"/><xs:complexType name="sayHi"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="sayHiResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="getCatsByUser"><xs:sequence><xs:element minOccurs="0" name="arg0" type="tns:user"/></xs:sequence></xs:complexType><xs:complexType name="user"><xs:sequence><xs:element minOccurs="0" name="address" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:int"/><xs:element minOccurs="0" name="name" type="xs:string"/><xs:element minOccurs="0" name="pass" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="getCatsByUserResponse"><xs:sequence><xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:cat"/></xs:sequence></xs:complexType><xs:complexType name="cat"><xs:sequence><xs:element minOccurs="0" name="color" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:int"/><xs:element minOccurs="0" name="name" type="xs:string"/></xs:sequence></xs:complexType></xs:schema></wsdl:types><wsdl:message name="getCatsByUserResponse"><wsdl:part element="ns1:getCatsByUserResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="sayHiResponse"><wsdl:part element="ns1:sayHiResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="getCatsByUser"><wsdl:part element="ns1:getCatsByUser" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="sayHi"><wsdl:part element="ns1:sayHi" name="parameters"></wsdl:part></wsdl:message><wsdl:portType name="HelloWorld"><wsdl:operation name="sayHi"><wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input><wsdl:output message="ns1:sayHiResponse" name="sayHiResponse"></wsdl:output></wsdl:operation><wsdl:operation name="getCatsByUser"><wsdl:input message="ns1:getCatsByUser" name="getCatsByUser"></wsdl:input><wsdl:output message="ns1:getCatsByUserResponse" name="getCatsByUserResponse"></wsdl:output></wsdl:operation></wsdl:portType></wsdl:definitions>

(二)语法详解:

1、根元素:definitions

1)targetNamespace :相当于java的package 。

如 服务端实现类与 wsdl 文件的targetNamespace为 如下,应该是一致的。

package com.ws.impl;//实现类的包targetNamespace="/"//wsdl的targetNamespace

2)xmlns :遵守的命名空间的schema 文件,相当于java的import 。即本文档引进了这个schema规范,需要遵守它的语法。可以有别名,用来区分引进不同的schema规范。

xmlns:xsd="/2001/XMLSchema"//xsd为别名

3)import :导入的接口文件。通过 namespase的路径也可以看出。

<wsdl:import location="http://localhost:8080/helloWorld?wsdl=HelloWorld.wsdl" namespace="/"></wsdl:import>

2、WS接口的wsdl语法。wsdl 显示的服务端内容都在此。

1)types元素 :该元素内容是标准的xml Schema文档 。

<wsdl:types><xs:schema xmlns:xs="/2001/XMLSchema" xmlns:tns="/" elementFormDefault="unqualified" targetNamespace="/" version="1.0"><xs:element name="getCatsByUser" type="tns:getCatsByUser"/><xs:element name="getCatsByUserResponse" type="tns:getCatsByUserResponse"/><xs:element name="sayHi" type="tns:sayHi"/><xs:element name="sayHiResponse" type="tns:sayHiResponse"/><xs:complexType name="sayHi"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="sayHiResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="getCatsByUser"><xs:sequence><xs:element minOccurs="0" name="arg0" type="tns:user"/></xs:sequence></xs:complexType><xs:complexType name="user"><xs:sequence><xs:element minOccurs="0" name="address" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:int"/><xs:element minOccurs="0" name="name" type="xs:string"/><xs:element minOccurs="0" name="pass" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="getCatsByUserResponse"><xs:sequence><xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:cat"/></xs:sequence></xs:complexType><xs:complexType name="cat"><xs:sequence><xs:element minOccurs="0" name="color" type="xs:string"/><xs:element minOccurs="0" name="id" type="xs:int"/><xs:element minOccurs="0" name="name" type="xs:string"/></xs:sequence></xs:complexType></xs:schema></wsdl:types>

2)message元素:有2N个message元素,需要有传入传出消息。如服务端方法public String sayHi(String name){}方法:

<wsdl:message name="getCatsByUserResponse"><wsdl:part element="ns1:getCatsByUserResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="sayHiResponse"><wsdl:part element="ns1:sayHiResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="getCatsByUser"><wsdl:part element="ns1:getCatsByUser" name="parameters"></wsdl:part></wsdl:message><span style="color:#ff0000;"><wsdl:message name="sayHi"><wsdl:part element="ns1:sayHi" name="parameters"></wsdl:part></wsdl:message></span>

①、message 元素中有name等于sayHi 的属性。可以理解为 一个sayHi 的消息。

②、子元素 part中有element属性为sayHi 。理解为sayHi的消息需要一个sayHi 的元素(element)。这个元素在上面的 type 中会有定义。

3)portType元素: N个operation子元素,每个operation代表一个ws操作(即方法),包含请求与回复2次。

<wsdl:portType name="HelloWorld"><wsdl:operation name="sayHi"><wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input><wsdl:output message="ns1:sayHiResponse" name="sayHiResponse"></wsdl:output></wsdl:operation><wsdl:operation name="getCatsByUser"><wsdl:input message="ns1:getCatsByUser" name="getCatsByUser"></wsdl:input><wsdl:output message="ns1:getCatsByUserResponse" name="getCatsByUserResponse"></wsdl:output></wsdl:operation></wsdl:portType>

①、服务端有两个操作的方法sayHi() 和getCatsByUser() ,在portType 元素中就会有 2个operation ,分别为sayHi 和getCatsByUser 。

②、每个operation元素中有input 和 output 子元素,定义了这次操作需要的 message元素信息,在上面定义。

4)显而易见,这边有一定的依赖关系存在:一个 WS 服务端实现类,定义了多个方法(operation元素),方法的调用需要明确传入传出参数,被定义在message元素中,称为消息。每个定义的消息有依赖于element元素,定义在 types 中,是一份标准的 schema。

5)整个接口的wsdl 由于上述依赖的关系,可以倒过来分析:如方法public List<Cat> getCatsByUser(User user) {},其实其本质规定了调用该方法传入参数的类型与规范。

①、一个接口,定义方法为getCatsByUser。wsdl 定义为:一个portType 为HelloWorld 的接口,有名为getCatsByUser的operation,即有这个方法可调用。这个方法需要name为getCatsByUser 和 getCatsByUserResponse的 message 元素(input 和 output ),代表了这个方法的传入与传出消息(即方法的参数)。

②、方法的传入传出参数。message 元素中规定这个getCatsByUser 的传入消息实际内容在名为getCatsByUser的element元素中,element元素定义在types元素中。

③、element元素中规定getCatsByUser 遵守名字为getCatsByUser 的schema语法定义,使用type关键字实现sechema语法的复用。

④、名字为getCatsByUser 的schema定义为:complex为复杂类型,0-1个,sequesce指要有顺序,type指向名字为 user 的schema定义。

⑤、名字为user的schema定义为: 有顺利的四个元素,分别为address、id、name、pass,出现0-1次。

4、本质:一个ws,其实并不是方法的调用,而是发送soap消息(即xml文档片段)

1)客户端把调用的方法参数,转换生成xml文档片段(soap消息)??必须符合wsdl规定的格式

2)客户端通过网络,把xml文档片段传给服务器。

3)服务器接收到xml文档片段。

4)服务器解析xml文档片段,提取其中的数据。返回xml文档。

如getCatsByUser(User user) 方法,传入传出的是这样的xml片段:

<!--传入的参数--><getCatsByUser><arg0><address></address><id></id><name></name><pass></pass></arg0></getCatsByUser><!--返回的参数--><getCatsByUserResponse><return><color></color><id></id><name></name></return><return><color></color><id></id><name></name></return></getCatsByUserResponse>

3、WS实现类:

1)、binding:指定ws的函数风格,并详细的定义了接口中的操作即operation。函数风格,现在一般为 document 文档风格。

<wsdl:binding name="HelloSAMSoapBinding" type="ns1:HelloWorld"><soap:binding <span style="color:#ff0000;">style="document" </span>transport="/soap/http"/><<span style="color:#ff0000;">wsdl:operation </span>name="sayHi"><soap:operation soapAction="" style="document"/><wsdl:input name="sayHi"><soap:body use="literal"/></wsdl:input><wsdl:output name="sayHiResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getCatsByUser"><soap:operation soapAction="" style="document"/><wsdl:input name="getCatsByUser"><soap:body use="literal"/></wsdl:input><wsdl:output name="getCatsByUserResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding>

2)、service:name-定义了ws的名称,即代码中使用serviceName 指定的名称,port【address】-定义ws绑定的地址。

<wsdl:service name="HelloSAM"><wsdl:port binding="tns:HelloSAMSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:8080/helloWorld"/></wsdl:port></wsdl:service>

二、SOAP语言(简单访问对象协议)

(一)通过使用CXF的日志拦截器,我们可以在控制台输出拦截器的soap消息。服务端拦截如下:

1、sayHi()操作的soap消息:

传入:Headers: {Accept=[text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Length=[186], content-type=[text/xml; charset=UTF-8], Host=[localhost:8080], SOAPAction=[""], User-Agent=[Java/1.6.0_10-rc2]}Payload:<?xml version="1.0" ?><S:Envelope xmlns:S="/soap/envelope/"><S:Body><ns2:sayHi xmlns:ns2="/"><arg0>SAM-SHO</arg0></ns2:sayHi></S:Body></S:Envelope>传出:Headers: {}Payload: <soap:Envelope xmlns:soap="/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="/"><return>SAM-SHO,您好!您现在访问的是简单的WS服务端,时间为:14-11-20 下午1:28</return></ns2:sayHiResponse></soap:Body></soap:Envelope>

2、getCatsByUser()操作的soap消息:与上面wsdl分析的消息应该是一致的。

传入:<?xml version="1.0" ?><S:Envelope xmlns:S="/soap/envelope/"><S:Body><ns2:getCatsByUser xmlns:ns2="/"><arg0><address>soochow</address><id>1</id><name>Sam-Sho</name><pass>1234</pass></arg0></ns2:getCatsByUser></S:Body></S:Envelope>传出:<soap:Envelope xmlns:soap="/soap/envelope/"><soap:Body><ns2:getCatsByUserResponse xmlns:ns2="/"><return><color>黄色</color><id>1</id><name>加菲猫</name></return><return><color>蓝色</color><id>2</id><name>蓝胖子</name></return><return><color>粉色</color><id>3</id><name>hello kitty</name></return><return><color>黑白色</color><id>4</id><name>熊猫</name></return></ns2:getCatsByUserResponse></soap:Body></soap:Envelope>

(二)soap语法

<soap:Envelope xmlns:soap="/soap/envelope/"><soap:Header><!--可能会有Header元素--></soap:Header><soap:Body><ns2:sayHiResponse xmlns:ns2="/"><return>SAM-SHO,您好!您现在访问的是简单的WS服务端,时间为:14-11-20 下午1:28</return></ns2:sayHiResponse></soap:Body></soap:Envelope>

1、根元素:Envelope。

2、Header元素::不是强制出现,由程序员控制,主要用于携带一些额外的信息,比如用户名、密码

3、Body:

1)调用正确,body元素内容应该遵守WSDL要求的格式。

<soap:Envelope xmlns:soap="/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="/"><return>SAM,您好现在的时间是:Thu Jan 09 10:19:57 CST </return></ns2:sayHiResponse></soap:Body></soap:Envelope>

2)调用错误,body元素内容显示Faulty元素。

<soap:Fault><faultcode>soap:Server</faultcode><faultstring>No binding operation info while invoking unknown method with params unknown.</faultstring></soap:Fault></soap:Body>

4、return:返回信息。

三、UDDI(可以省略)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。