WebService概述
WebService是什么
WebService就是一种跨编程语言和跨操作系统平台的远程调用技术。所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然。跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。
WebService能做什么
- 不同系统、平台、语言之间相互远程通信和远程调用
- 可以整合不同的业务系统
WebService两种类型,
- 一种是以SOAP协议风格的WebService
- 一种是Restful风格的Webservice
WebService核心元素
- SOAP:简单对象协议
- WSDL:WebService描述语言
- UUDI:统一描述、发现和集成协议
SOAP
SOAP:即简单对象访问协议,是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。
构建模块
- 必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息
- 可选的 Header 元素,包含头部信息
- 必需的 Body 元素,包含所有的调用和响应信息
- 可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
语法规则
- 根元素:Envelope
- Header元素:不是强制出现,由程序员控制,主要用于携带一些额外的信息,比如用户名、密码
- Body:
1) 调用正确,body元素内容应该遵守WSDL要求的格式。
2) 调用错误,body元素内容显示Faulty元素。
WSDL文件解析
WSDL总体描述
WSDL(Web服务描述语言,Web Services Description Language)是描述Web服务发布的XML格式。W3C组织(World Wide Web Consortium)没有批准1.1版的WSDL,当前的WSDL版本是2.0,是W3C的推荐标准(recommendation)(一种官方标准),并将被W3C组织批准为正式标准。
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.cxfdemo.fengfan.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="cxfWs" targetNamespace="http://service.cxfdemo.fengfan.com">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.cxfdemo.fengfan.com" elementFormDefault="unqualified" targetNamespace="http://service.cxfdemo.fengfan.com" version="1.0">
<xs:element name="hello" type="tns:hello"/>
<xs:element name="helloResponse" type="tns:helloResponse"/>
<xs:complexType name="hello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="helloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="hello">
<wsdl:part element="tns:hello" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="helloResponse">
<wsdl:part element="tns:helloResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="CXFWebService">
<wsdl:operation name="hello">
<wsdl:input message="tns:hello" name="hello"> </wsdl:input>
<wsdl:output message="tns:helloResponse" name="helloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="cxfWsSoapBinding" type="tns:CXFWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="hello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="hello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="helloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="cxfWs">
<wsdl:port binding="tns:cxfWsSoapBinding" name="CXFWebServiceImplPort">
<soap:address location="http://127.0.0.1:8080/cxfDemo/services/cxfWs"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
元素 | 含义 |
---|---|
Types(消息类型) | 数据类型定义的容器,它使用某种类型系统(如 XSD)。 |
Message(消息) | 通信数据的抽象类型化定义,它由一个或者多个 part 组成。 |
Part | 消息参数 |
PortType(端口类型) | 特定端口类型的具体协议和数据格式规范。,它由一个或者多个 Operation组成。 |
Operation(操作) | 对服务所支持的操作进行抽象描述 |
Binding | 特定端口类型的具体协议和数据格式规范。 |
Port | 定义为绑定和网络地址组合的单个端点。 |
Service | 相关端口的集合,包括其关联的接口、操作、消息等。 |
definitions元素
所有的WSDL文档的根元素均是definitions元素。该元素封装了整个文档,同时通过其name提供了一个WSDL文档。除了提供一个命名空间(targetNamespace)外,该元素没有其他作用,故不作详细描述。
types元素
WSDL采用了W3C XML模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型。当声明消息部分的有效时,消息定义使用了在types元素中定义的数据类型和元素。
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.cxfdemo.fengfan.com" elementFormDefault="unqualified" targetNamespace="http://service.cxfdemo.fengfan.com" version="1.0">
<xs:element name="hello" type="tns:hello"/>
<xs:element name="helloResponse" type="tns:helloResponse"/>
<xs:complexType name="hello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="helloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
上面是数据定义部分,该部分定义了两个元素:hello、helloResponse
- hello:定义了一个复杂类型,包含一个字符串和一个int类型,将来用来描述操作的参数传入部分。<xs:element minOccurs=“0” name=“arg0” type=“xs:string”/>、<xs:element name=“arg1” type=“xs:int”/>,是确定传入参数是String类型和Int类型
- helloResponse:定义了一个复杂类型,仅仅包含一个简单的字符串,将来用来描述操作的返回值。<xs:element minOccurs=“0” name=“return” type=“xs:string”/>确定方法hello返回的类型是String类型的。
import元素
import元素使得可以在当前的WSDL文档中使用其他WSDL文档中指定的命名空间中的定义元素。本例子中没有使用import元素。通常在用户希望模块化WSDL文档的时候,该功能是非常有效果的。
<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/>
message元素
message元素描述了Web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载;还可以描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用RPC样式还是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递。
<wsdl:message name="hello">
<wsdl:part element="tns:hello" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="helloResponse">
<wsdl:part element="tns:helloResponse" name="parameters"> </wsdl:part>
</wsdl:message>
WebService中每个方法包含两部分:
- 一个是方法的输入参数;另一个是方法的输出参数。
- 其实质都是基于SOAP协议将其封装为消息,所以每一个方法对应有两个消息,一个输入一个输出回应。简单而言,就是方法和Message的关系是N:2N的关系。一对二。
- Message中的具体内容是part,结合前面可知,message中的part内容请到前面定义过的types中去看,它会引用之前的type相关内容
portType元素
portType元素定义了Web服务的抽象接口。该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来说明Web服务实现使用的Internet协议、编码方案以及Internet地址,一个portType中可以定义多个operation,一个operation可以看作是一个方法。
<wsdl:portType name="CXFWebService">
<wsdl:operation name="hello">
<wsdl:input message="tns:hello" name="hello"> </wsdl:input>
<wsdl:output message="tns:helloResponse" name="helloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
binding元素
binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。通常binding元素与协议专有的元素和在一起使用。
<wsdl:binding name="cxfWebServiceSoapBinding" type="tns:CXFWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="hello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="hello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="helloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
service元素和port元素
service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。
<wsdl:service name="cxfWebService">
<wsdl:port binding="tns:cxfWebServiceSoapBinding" name="CXFWebServiceImplPort">
<soap:address location="http://127.0.0.1:8080/cxfDemo/services/cxfWebService"/>
</wsdl:port>
</wsdl:service>
UDDI
Universal Description, Discovery and Integration
为加速Web Service的推广、加强Web Service的互操作能力而推出的一个计划,基于标准的服务描述和发现的规范(specification)。
以资源共享的方式由多个运作者一起以Web Service的形式运作UDDI商业注册中心。
UDDI计划的核心组件是UDDI商业注册,它使用XML文档来描述企业及其提供的Web Service。
UDDI商业注册提供三种信息:
White Page包含地址、联系方法、已知的企业标识。
Yellow Page包含基于标准分类法的行业类别。
Green Page包含关于该企业所提供的Web Service的技术信息,其形式可能是指向文件或URL的指针,而这些文件或URL是为服务发现机制服务的。