正 文

理解 Web 服务的开发模式


www.7dspace.com  更新日期:2006-1-14 6:10:30  七度空间


  使用 Web 服务工具从该 Java 类生成 Web 服务实现。有几个完善程度各不相同的候选工具,它们能够生成 JAX-RPC 在部署和使用 Web 服务的过程中所需的全部构件或部分构件。这些工具都能够:基于步骤 2 中的 Java 类的签名生成 WSDL 文档;为该 Java 类的公共方法所使用的每个数据类型 (JavaBean) 创建一个嵌入到该 WSDL 文档的 types 元素中的复杂类型定义;为该 Java 类的每个公共方法在 portType 元素中创建一个 operation 元素;生成将该实现部署为应用程序服务器上的 Web 服务所需的任何其他提供程序端构件。

  这些工具中的低端工具包括有基于 Apache Axis 和 IBM WebSphere Application Server 中的 Java2WSDL 工具的命令行。这些低端工具只能生成主要的构件,例如 WSDL 文件和部署代码。高端工具的例子是 GUI 驱动的 Web 服务向导,它包含在企业应用程序开发工具的 IBM® Eclipse-based WebSphere® Studio 系列(如 Application Developer)中。该 Web 服务向导能够生成所有 JAX-RPC 构件和目标应用程序服务器的特定构件。实际上,如果需要,该向导甚至可以部署和启动 Web 服务。

  清单 3. 在所生成的 HelloWorld.wsdl 的 types 元素内生成的 schema 元素

<schema elementFormDefault="qualified" targetNamespace="http://dto.hello.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:impl="http://hello.com" xmlns:intf="http://hello.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <complexType name="HelloWorldData">
     <sequence>
     <element name="value" nillable="true" type="xsd:string" />
            </sequence>
 </complexType>
</schema>

  清单 4. Java2WSDL 为服务器端生成的构件列表

Java Resources/com/hello/HelloWorld_SEI.java - Service Endpoint Interface
Java Resources/com/hello/dto/HelloWorldData_Deser.jav - Deserialize DTO from XML message
Java Resources/com/hello/dto/HelloWorldData_Helperjava - Helper class that contains DTO metadata
Java Resources/com/hello/dto/HelloWorldData_Ser.java - Serialize DTO into XML message
WebContent/WEB-INF/wsdl/HelloWorld.wsdl - WSDL returned from ?wsdl HTTP request
WebContent/WEB-INF/webservices.xml - JAX-RPC server deployment descriptor
WebContent/WEB-INF/web.xml - Web services servlet definition
WebContent/WEB-INF/ibm-webservices-bnd.xmi - WebSphere-specific binding extensions
WebContent/WEB-INF/ibm-webservices-ext.xmi -  WebSphere-specific extensions
WebContent/WEB-INF/HelloWorld_mapping.xml - WSDL to Java mapping metadata
WebContent/wsdl/com/hello/HelloWorld.wsdl - Copy of WSDL required by some runtimes such as process choreography

  使用 Web 服务工具为该 Web 服务请求程序生成构件(使用步骤 3 中生成的 WSDL 文档)。这些工具能够:为每个定义在 WSDL 的 types 元素内的复杂类型生成一个 Java 类 (DTO);生成一个服务端点接口 (SEI),该接口含有 portType 元素内的每个操作的方法;生成一个存根来实现该 SEI,客户机可以使用该存根将请求发送到 Web 服务实现,并接收来自 Web 服务实现的响应。这些工具还可以生成其他构件,具体情况依赖于请求程序的性质(是否为 J2EE 容器)和工具本身。生成提供程序实现的同类工具通常还会有一个请求程序版本。因此,工具的范围相同,从 Apache Axis 或 Application Server 中的命令行驱动的 WSDL2Java 到 WebSphere Studio Application Developer 中的 Web 服务向导。

  清单 5. Java2WSDL 为客户机端生成的构件列表

Java Resources/com/hello/HelloWorld.java - Service Endpoint Interface for client
Java Resources/com/hello/HelloWorldProxy.java - Client Proxy implementing interface
Java Resources/com/hello/HelloWorldService.java - Interface for client-side service locator
Java Resources/com/hello/HelloWorldServiceLocator.java - Implementation for service locator
Java Resources/com/hello/HelloWorldSoapBindingStub.java - Client-side Stub implementation
Java Resources/com/hello/dto/HelloWorldData.java - Client-side DTO implementation
Java Resources/com/hello/dto/HelloWorldData_Deser.java - Deserialize DTO from XML message
Java Resources/com/hello/dto/HelloWorldData_Helperjava - Helper class that contains DTO metadata
Java Resources/com/hello/dto/HelloWorldData_Ser.java - Serialize DTO into XML message
WebContent/WEB-INF/wsdl/HelloWorld.wsdl - WSDL for client request
WebContent/WEB-INF/webservicesclient.xml - JAX-RPC client deployment descriptor
WebContent/WEB-INF/ibm-webservicesclient-bnd.xmi - WebSphere-specific binding extensions
WebContent/WEB-INF/ibm-webservicesclient-ext.xmi - WebSphere-specific extensions
WebContent/WEB-INF/HelloWorld_mapping.xml - WSDL to Java mapping metadata

  从步骤 1 到步骤 3 必须由服务提供程序执行。步骤 4 必须由服务请求程序执行。服务请求程序只能在服务提供程序提供 WSDL(在步骤 3 中)后执行步骤 4。请注意,前三步所描述的 WSDL 不可能成为一个标准的、甚至可以公开使用的 WSDL。然而,步骤 4 的执行情况确实就像 WSDL 来自公共来源(如服务注册中心)一样。

  图 1. 自底向上的开发模式

5页,页码:[1] [2] [3] [4] [5] 

上一篇:轻松玩转Win2003的DHCP服务器
下一篇:试用Win2003 x64版:挑战Unix与Linux
标题:理解 Web 服务的开发模式 作者:Greg Flurry,Manish Modh 来源:developerWorks 中国
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐