SOAP、WSDL与UDDI协议
SOAP、WSDL(WebServicesDescriptionLanguage)、UDDI(UniversalDescriptionDiscovery andIntegration)是WebService三要素, SOAP用来描述传递信息的格式, WSDL 用来描述如何访问具体的接口, UDDI用来管理,分发,查询WebService 。具体实现可以搜索 Web Services简单实例 ; SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet上各种不同操作环境中的分布式对象。
WSDL
WSDL(Web服务描述语言,Web Services Description Language)是描述Web服务发布的XML格式。W3C组织(World Wide Web Consortium)没有批准1.1版的WSDL,当前的WSDL版本是2.0,是W3C的推荐标准(recommendation)(一种官方标准),并将被W3C组织批准为正式标准。
- WSDL是全完基于XML的,特别是xml schema。
- WSDL文档描述了 ws 主要的3个方面:
- WHATA:该 ws 包含”什么“操作,即有几个方法。
- HOW:该 ws 的操作应该”怎样“调用?
- WHERE:该 ws 的服务地址。
- WSDL 指网络服务描述语言
- WSDL 使用 XML 编写
- WSDL 是一种 XML 文档
- WSDL 用于描述网络服务
- WSDL 也可用于定位网络服务
- WSDL 还不是 W3C 标准
实现类的 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="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.com/" name="HelloSAM" targetNamespace="http://impl.ws.com/">
<wsdl:import location="http://localhost:8080/helloWorld?wsdl=HelloWorld.wsdl" namespace="http://ws.com/"></wsdl:import>
<wsdl:binding name="HelloSAMSoapBinding" type="ns1:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/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>
接口的 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="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://ws.com/" name="HelloWorld" targetNamespace="http://ws.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.com/" elementFormDefault="unqualified" targetNamespace="http://ws.com/" 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="http://impl.ws.com/"//wsdl的targetNamespace
2)xmlns :遵守的命名空间的schema 文件,相当于Java的 import 。即本文档引进了这个schema规范,需要遵守它的语法。可以有别名,用来区分引进不同的schema规范。
xmlns:xsd="http://www.w3.org/2001/XMLSchema"//xsd为别名
3)import :导入的接口文件。通过 namespase的路径也可以看出。
<wsdl:import location="http://localhost:8080/helloWorld?wsdl=HelloWorld.wsdl" namespace="http://ws.com/"></wsdl:import>
2、WS接口的wsdl语法。wsdl 显示的服务端内容都在此。
1)types元素 :该元素内容是标准的xml Schema文档 。
2)message元素:有2N个message元素,需要有传入传出消息。如服务端方法public String sayHi(String name){}方法:
- message 元素中有name等于 sayHi 的属性。可以理解为 一个 sayHi 的消息。
- 子元素 part中有element属性为 sayHi 。理解为 sayHi的消息需要一个 sayHi 的元素(element)。这个元素在上面的 type 中会有定义。
3)portType元素: N个operation子元素,每个operation代表一个ws操作(即方法),包含请求与回复2次。
- 服务端有两个操作的方法 sayHi() 和 getCatsByUser() ,在 portType 元素中就会有 2个 operation ,分别为 sayHi 和 getCatsByUser 。
- 每个 operation元素中有input 和 output 子元素,定义了这次操作需要的 message元素信息,在上面定义。
4)显而易见,这边有一定的依赖关系存在:一个 WS 服务端实现类,定义了多个方法(operation元素),方法的调用需要明确传入传出参数,被定义在message元素中,称为消息。每个定义的消息有依赖于element元素,定义在 types 中,是一份标准的 schema。
5)整个接口的 wsdl 由于上述依赖的关系,可以倒过来分析:如方法public List 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次。
3、本质:一个ws,其实并不是方法的调用,而是发送soap消息(即xml文档片段)
1)客户端把调用的方法参数,转换生成XML文档片段(soap消息),必须符合WSDL规定的格式
2)客户端通过网络,把xml文档片段传给服务器。
3)服务器接收到xml文档片段。
4)服务器解析xml文档片段,提取其中的数据。返回xml文档。
4、WS实现类:
1)binding:指定ws的函数风格,并详细的定义了接口中的操作即 operation 。函数风格,现在一般为 document 文档风格。
2)service:name-定义了ws的名称,即代码中使用 serviceName 指定的名称,port【address】-定义ws绑定的地址。
SOAP
简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。
语法规则
构建模块
- 一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:
- 必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息
- 可选的 Header 元素,包含头部信息
- 必需的 Body 元素,包含所有的调用和响应信息
- 可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
语法规则
这里是一些重要的语法规则:
- SOAP 消息必须用 XML 来编码
- SOAP 消息必须使用 SOAP Envelope 命名空间
- SOAP 消息必须使用 SOAP Encoding 命名空间
- SOAP 消息不能包含 DTD 引用
- SOAP 消息不能包含 XML 处理指令
消息基本结构
<?xml
version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
<!--百度百科示例-->
</soap:Header>
<soap:Body>
<!--百度百科示例-->
<soap:Fault>
<!--百度百科示例-->
</soap:Fault>
</soap:Body>
</soap:Envelope>
(一)通过使用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="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHi xmlns:ns2="http://ws.com/">
<arg0>SAM-SHO</arg0>
</ns2:sayHi>
</S:Body>
</S:Envelope>
传出:
Headers: {}
Payload:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://ws.com/">
<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="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getCatsByUser xmlns:ns2="http://ws.com/">
<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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getCatsByUserResponse xmlns:ns2="http://ws.com/">
<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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<!--可能会有Header元素-->
</soap:Header>
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://ws.com/">
<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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://ws.com/">
<return>SAM,您好现在的时间是:Thu Jan 09 10:19:57 CST 2014</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
UDDI是一种用于描述、发现、集成Web Service的技术,它是Web Service协议栈的一个重要部分。通过UDDI,企业可以根据自己的需要动态查找并使用Web服务,也可以将自己的Web服务动态地发布到UDDI注册中心,供其他用户使用。
参考文章:
https://baike.baidu.com/item/WSDL
https://blog.csdn.net/zhongguomao/article/details/51733161
https://baike.baidu.com/item/%E7%AE%80%E5%8D%95%E5%AF%B9%E8%B1%A1%E8%AE%BF%E9%97%AE%E5%8D%8F%E8%AE%AE/3841505?fromtitle=SOAP&fromid=4684413