Web Services 平台元素学习笔记

概述

Web Services 是一种通过网络进行互操作的应用程序接口(API),可以在不同的平台上使用。它使用基于 XML 的协议(如 SOAP)进行通信,提供了一种标准化的方式来访问服务。

在 Web Services 中,有三个主要的平台元素:SOAP、WSDL 和 UDDI。

SOAP

SOAP(简单对象访问协议)是一种基于 XML 的协议,用于在网络上交换结构化数据。它定义了一种格式,使得应用程序可以在分布式环境中进行通信。SOAP 消息通常在 HTTP 或 HTTPS 上发送,但也可以在其他传输协议上使用。

下面是一个简单的 SOAP 消息示例:

Copy Code
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header> <m:transactionId xmlns:m="http://example.com">12345</m:transactionId> </soap:Header> <soap:Body> <m:getUserRequest xmlns:m="http://example.com"> <m:userId>123</m:userId> </m:getUserRequest> </soap:Body> </soap:Envelope>

在这个示例中,消息包含一个事务 ID 和一个获取用户请求。

WSDL

WSDL(Web Services Description Language)是一种基于 XML 的语言,用于描述 Web 服务的接口和实现。它包含了服务的操作、输入和输出参数、消息格式和协议细节等信息。

下面是一个简单的 WSDL 文档示例:

Copy Code
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://example.com/UserService/"> <wsdl:types> <xsd:schema targetNamespace="http://example.com/UserService/"> <xsd:element name="getUserRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="userId" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getUserResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="user" type="tns:User"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="User"> <xsd:sequence> <xsd:element name="id" type="xsd:int"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message name="GetUserRequest"> <wsdl:part name="parameters" element="tns:getUserRequest"/> </wsdl:message> <wsdl:message name="GetUserResponse"> <wsdl:part name="parameters" element="tns:getUserResponse"/> </wsdl:message> <wsdl:portType name="UserServicePort"> <wsdl:operation name="getUser"> <wsdl:input message="tns:GetUserRequest"/> <wsdl:output message="tns:GetUserResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="UserServiceBinding" type="tns:UserServicePort"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getUser"> <soap:operation soapAction="http://example.com/UserService/getUser"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="UserService"> <wsdl:port name="UserServicePort" binding="tns:UserServiceBinding"> <soap:address location="http://example.com/UserService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

在这个示例中,文档描述了一个名为 getUser 的操作和它的输入输出参数。它还定义了一个名为 UserServicePort 的端口类型和与之对应的绑定和服务。

UDDI

UDDI(Universal Description, Discovery and Integration)是一种用于发布、发现和集成 Web 服务的标准化目录服务。它提供了一个集中的位置,使得用户可以搜索和使用可用的 Web 服务。

下面是一个简单的 UDDI 文档示例:

Copy Code
<uddi:businessList xmlns:uddi="urn:uddi-org:api_v3"> <uddi:businessInfo> <uddi:name>Example</uddi:name> <uddi:businessKey>uuid:685b9f16-3e77-11e4-a6c7-123b93f75cba</uddi:businessKey> <uddi:description> This is an example UDDI business. </uddi:description> </uddi:businessInfo> <uddi:bindingTemplate> <uddi:accessPoint useType="endPoint">http://example.com/UserService</uddi:accessPoint> </uddi:bindingTemplate> <uddi:bindingTemplate> <uddi:accessPoint useType="webService">http://example.com/UserService?wsdl</uddi:accessPoint> <uddi:bindingKey>uuid:685b9f16-3e77-11e4-a6c7-123b93f75cba</uddi:bindingKey> <uddi:serviceKey>uuid:685b9f16-3e77-11e4-a6c7-123b93f75cba</uddi:serviceKey> </uddi:bindingTemplate> <uddi:serviceInfo> <uddi:name>User Service</uddi:name> <uddi:serviceKey>uuid:685b9f16-3e77-11e4-a6c7-123b93f75cba</uddi:serviceKey> </uddi:serviceInfo> </uddi:businessList>

在这个示例中,文档描述了一个名为 User Service 的 Web 服务,并提供了它的访问点和相关的绑定和服务信息。

实例

一个常见的 Web Services 实例是银行账户的转账操作。下面是一个简单的示例:

  1. 使用 SOAP 消息发送一个转账请求到银行账户 Web 服务。
  2. Web 服务解析请求并验证授权和账户余额。
  3. 如果请求被授权并且余额足够,Web 服务向银行系统发送一个转账请求。
  4. 银行系统处理请求并返回结果给 Web 服务。
  5. Web 服务解析结果并返回一个响应消息给客户端。

在这个示例中,SOAP 被用来传输请求和响应消息,WSDL 被用来描述 Web 服务接口和数据类型,UDDI 可以被用来集成不同的银行系统和 Web 服务提供商。