[技术| 编程·课件·Linux] 【网络】webservice快速入门-SOAP和WSDL

admin · 发布于 2013-12-13 15:10 · 1316 次阅读
1

什么是SOAP?SOAP:Simple Object Access Protocol

以下是百度的结果:

SOAP:简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序

说白了,它就是一种基于XML传输数据的协议,为什么基于XML,因为这样可以确保不同平台,语言的通信,也就是经常听到的导构平台之前的通信。

我们常见的json,xml其实都可以理解为是soap的实现。

我们来看一下之前的WSDL文件,访问:http://localhost:7777/tudou?wsdl如下:


[XML] 纯文本查看 复制代码
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 [url]http://jax-ws.dev.java.net.[/url] RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<!--
 Generated by JAX-WS RI at [url]http://jax-ws.dev.java.net.[/url] RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.platform.whaty.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.platform.whaty.com/" name="MyServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://server.ws.platform.whaty.com/" schemaLocation="http://localhost:7777/tudou?xsd=1"/>
</xsd:schema>
</types>
<message name="minus">
<part name="parameters" element="tns:minus"/>
</message>
<message name="minusResponse">
<part name="parameters" element="tns:minusResponse"/>
</message>
<message name="add">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<portType name="IMyservice">
<operation name="minus">
<input message="tns:minus"/>
<output message="tns:minusResponse"/>
</operation>
<operation name="add">
<input message="tns:add"/>
<output message="tns:addResponse"/>
</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:IMyservice">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="minus">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyServiceImplService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:7777/tudou"/>
</port>
</service>
</definitions>


我们把节点收起来看得更清楚点:


很清楚的看到这个wsdl分为type,message,porttype,binding,service这5部分。

type:用来定义访问的类型,一个类型对应我们服务端接口的一个方法,一个类型对应我们接口的一个返回值。我们可以看到上面的wsdl中有一个schemaLocation="http://localhost:7777/tudou?xsd=1"的玩意,我们直接在浏览器访问一下它。

[XML] 纯文本查看 复制代码
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 [url]http://jax-ws.dev.java.net.[/url] RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<xs:schema xmlns:tns="http://server.ws.platform.whaty.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://server.ws.platform.whaty.com/">
<xs:element name="add" type="tns:add"/>
<xs:element name="addResponse" type="tns:addResponse"/>
<xs:element name="minus" type="tns:minus"/>
<xs:element name="minusResponse" type="tns:minusResponse"/>
<xs:complexType name="add">
<xs:sequence>
<xs:element name="arg0" type="xs:int"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="minus">
<xs:sequence>
<xs:element name="arg0" type="xs:int"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="minusResponse">
<xs:sequence>
<xs:element name="return" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

看到complexType了没有。如果你学了schema和dtd,你就应该知道这实际就是一种类型,下面是相应的子节点,可能还有一些约束。


message:SOAP,也就是被封装成一个对象的形式,实际上是以XML的形式展现的。里面就是传递的我们的数据。

portType:就是对应我们的操作了。可以看到operation这个单词吧。

binding:指定消息所使用的格式。Literal就是不在SOAP消息中表明数据类型,而通过其它方式获知数据类型,这种方式是开发包相关的,没有什么标准;如<x>50</x>,单从SOAP消息,你无法判断50是数字还是字符串。详见:

[XML] 纯文本查看 复制代码
http://blog.csdn.net/jackyrongvip/article/details/4608014

而它里面的style="Document"中Document就是将SOAP请求和响应,或者说输入输出定义为XML元素,有严格的Schema("document" style means the messages in and out of the service are exactly as they are describe by the XML Schema in the WSDL).

service:指定服务发布的名称。



共收到 1 条回复
xywhere · #2 · 2013-12-15 12:17:24  回复
沙发
回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表