用 WSAS 和 Eclipse 简化 Web 服务的创建

用 WSAS 和 Eclipse 简化 Web 服务的创建

需要快速创建 Web 服务?请考虑使用 Eclipse 的 Web Services Application Server 插件

developerWorks
文档选项
<script language="JavaScript" type="text/javascript"> </script>
将打印机的版面设置成横向打印模式

打印本页

将此页作为电子邮件发送

将此页作为电子邮件发送


级别: 中级

Michael Galpin (mike.sr@gmail.com), 开发人员, eBay

2008 年 4 月 15 日

现在 Web 服务无处不在。如果需要为正在开发的新应用程序或是新特性创建 Web 服务,这是一个好机会。而在不久前,创建 Web 服务还是一项费力的工作。WSO2 意识到了这种情况并创建了一个 Eclipse 插件来帮助您更轻松地使用 Web 服务。本文将介绍 WSO2 Web Services Application Server (WSAS) Eclipse 插件以及它如何帮助开发 Web 服务。

WSO2 WSAS 是什么?

在深入介绍适用于 Eclipse 的 Web Services Application Server (WSAS) 插件之前,需要先讨论一下 WSAS 是什么。WSAS 是针对 Web 服务的 Java™ 应用服务器。它是在 Apache Software License 下发布的开源项目。WSAS 包含适用于需要用 Web 服务完成的所有任务(包括创建、部署、管理等)的工具。它提供了尤为有效的工具,使用这些工具可以在现代 Web 服务(包括您听说过的各种 WS-*)中启用期望的许多功能。WSAS 是一个相对新的产品,但是它构建在经过检验的开源技术(包括 Axis2 和 Apache Tomcat)之上。

您可以将 WSAS 与其他应用服务器结合使用,例如 Tomcat 或 IBM® WebSphere®,但是它也可以完全独立运行。在本文中,我们将以独立模式使用 WSAS,主要介绍作为 WSAS 的一部分的 Eclipse 插件,并查看如何用该 Eclipse 插件更轻松地创建 Web 服务及它如何与 WSAS 绑定在一起。

WSAS —— 及 Web 服务标准本身 —— 背后的一些开发人员在 WSO2 工作,WSO2 是一家基于 Apache 项目开发开源软件并提供支持、咨询和培训服务的公司。从 WSO2 的开发者门户 WSO2 Oxygen Tank 下载 WSAS(请参阅 参考资料)。





回页首


系统要求

需要配备 Eclipse V3.3、WSO2 WSAS 和 WSAS Eclipse 插件才能开发 Web 服务。WSAS 插件将利用 Eclipse Web Tools Platform。满足这项要求的最简单方法是使用 Eclipse Java EE。您还需要 WSO2 的 WSAS V2.1 和 Java Development Kit (JDK) V5.0 或更高版本(请参阅 参考资料)。本文使用了 Mac OS X,但是所有软件都要求能在 Windows® 和 Linux® 中运行。根据操作系统的不同,可能需要调整文件位置。





回页首


Eclipse 插件

WSAS 拥有一个功能强大的管理应用程序,稍后我们就会看到。它支持把各种基于 JAR 的归档部署成 Web 服务,甚至还支持把遗留的 Web 服务部署描述符与 Axis 结合使用。但是,您仍需要编写代码并创建这些不同工件。开发 Java 应用程序和使用 Eclipse 密不可分。并且使用 WSAS 插件可以轻松地把 Java 应用程序转换为运行在 WSAS 上的 Web 服务。但是在开始使用它之前,需要先进行安装和设置。

安装

要安装 WSAS 插件,必须已经安装了 Eclipse 和 WSAS。您还需要设置 JAVA_HOME 环境变量(如果尚未设置)。此环境变量应当指向 JDK 的安装目录。设置完成后,请转到 WSAS 安装的 bin 目录并运行安装脚本。对于 Windows,此脚本为 install.bat,对于 Linux 和 OS X,此脚本为 install.sh。您应当收到清单 1 中所示的输出。


清单 1. 运行安装程序脚本
                
>./install.sh 
Running WSO2 Web Services Application Server, v2.1 installer...

###########################################################
#                                                         #
#               WSO2 WSAS v2.1 Installation               #
#                                                         #
###########################################################


Please select your installation mode: 
1) Eclipse WTP Plugin Installation
2) Servlet Container Installation
3) Windows NT Service Installation
4) Windows NT Service Uninstallation
:

选择第 1 个选项,然后应当会看到清单 2 中所示的输出。


清单 2. 选择 Eclipse 主页
                
Selection: Eclipse WTP Plugin Installation

Starting WSAS Eclipse WTP Plugin installation...
Please shutdown the Eclipse instance, If Already Running ...
Please enter Eclipse WTP Home :

如述,WSAS 插件将利用 Eclipse WTP。对于此提示,通常只需输入 Eclipse 的安装目录。惟一的例外情况是您把 Eclipse 设置为将插件放在 Eclipse 安装目录之外。这种情况下,请输入外部插件目录。无论如何,您都应当能够看到下列输出。


清单 3. 安装了 WSAS 插件
                
Copying WSAS Eclipse WTP Plugins /Applications/eclipse/plugins
OK

WSO2 WSAS Eclipse WTP Plugin installation was successful.
Please restart Eclipse WTP Instance..

以上是安装 WSAS 插件的全部过程。如果现在启动 Eclipse,则应当会看到 WSAS 工具和菜单,如下图所示:


图 1. WSAS IDE
WSAS IDE

现在 WSAS 插件已安装,接下来需要配置 WSAS 插件以便我们可以开始使用。

配置

要配置 WSAS 插件,需要打开 Eclipse Preferences 并打开 Web Services > WSAS Preferences,如下所示:


图 2. WSAS Preferences 面板
WSAS Preferences 面板

我们只有一件事需要做:选择 WSAS Runtime。选定后,WSAS 插件将检验该位置并显示一条表示它装载了 WSAS 运行时的消息。完成后,您可以单击 OK。现在您已经准备好开始使用 WSAS 插件开发 Web 服务。





回页首


开发 Web 服务

到目前为止,所完成的全部工作是安装 WSAS 插件并通过告诉插件 WSAS 的安装位置来配置插件。这将使您可以开始使用 WSAS 插件开发 Web 服务。您将获得一个简单的 Java 类并使用 WSAS 插件把它转换为 Web 服务。然后使用 WSAS 测试 Web 服务。

使用 POJO

Web 服务已有多年历史。只要有 Web 服务,就有创建 Web 服务的工具包和框架。这些框架的功能通常涉及声明接口、实现框架所定义的接口或者创建用于生成代码的 XML 文件(或以上所有功能)。现代 Web 服务则完全不同。普通 Java 对象(Plain Old Java Object,POJO)一词通常用来指将普通 Java 类与诸如 Hibernate 之类的持久性框架结合使用,但是它们同样适用于 Web 服务。所有 POJO 都可以成为使用 WSAS 插件的 Web 服务。因此对于我们来说,要开发 Web 服务,只需编写 Java 类。我们将编写一个确定整数因子的类。清单 4 显示了该类的代码。


清单 4. FactorService
                
package org.developerworks.services;

import java.util.LinkedList;
import java.util.List;

public class FactorService {
     public Integer[] factor(int num){
          List<Integer> factors = new LinkedList<Integer>();
          int sqrt = (int) Math.floor(Math.sqrt(num));
          for (int i=1;i<=sqrt;i++){
               if (num % i == 0){
                    int mid = factors.size()/2;
                    factors.add(mid, i);
                    int quotient = num/i;
                    if (i != quotient){
                         factors.add(mid+1, quotient);
                    }
               }
          }
          return factors.toArray(new Integer[factors.size()]);
     }
}

下面是该类如何工作的简单说明。它接受一个整数进行分解。对输入的整数求平方根,遍历所有小于或等于平方根的整数。如果该整数可以除输入整数,则不仅要加上该整数而且要加上除以计数整数的输入整数,并将它们添加到一个列表中。它将按顺序添加整数,因此在结束时已经给列表排好序。然后它将把列表转换成返回的数组。这无疑是 POJO:没有实现任何接口 —— 只有应用程序逻辑。这就像开发一样简单,因为除了实现业务逻辑之外我们什么都没做(计算整数因子)。因此把这一切转换成 Web 服务有多么简单?让我们使用 WSAS 插件并一探究竟。

创建和部署 Web 服务

要创建一个新 Web 服务,首先创建一个 Web 应用程序。为此,选择 File > New > Other,如图 3 所示:


图 3. 启动新 Web 应用程序向导
启动新 Web 应用程序向导

现在选择 Web > Dynamic Web Project


图 4. 新建动态 Web 项目
新建动态 Web 项目

这将打开 New Dynamic Web Project 界面。


图 5. 新建动态 Web 项目
新建动态 Web 项目

您应当注意到的一件事是没有 Target Runtime。我们需要指定 WSAS 作为目标运行时,因此单击 New。这将打开 New Server Runtime 界面。


图 6. 新建服务器运行时
新建服务器运行时

在 New Server Runtime 界面中,确保指定 WSO2 > WSO2 WSAS 作为运行时类型。另外,确保选中 “Also create new local server” 旁边的复选框,然后单击 Finish。这样做应当会重定向到 Dynamic Web Project 屏幕。


图 7. 指定了服务器运行时的动态 Web 项目
指定了服务器运行时的动态 Web 项目

一定要单击 Next 而不要单击 Finish。这应当会打开 Project Facets 界面。


图 8. Project Facets
Project Facets

确保选择 WSAS Web Service 方面。在这里,继续并单击 Finish。这将为您创建一个骨架 Web 项目。现在可以把 FactorService 类添加到这个项目中(File > New > Class)。您还可以通过 FactorService 类创建 Web 服务。再次选择 File > New > Other,但是这一次选择 Web Services > Web Service


图 9. 新建 Web 服务
新建 Web 服务

单击 Next 将打开新 Web 服务界面。


图 10. 定义新 Web 服务
定义新 Web 服务

注意在 Configuration 下,Web 服务运行时被选为 Apache Axis。我们需要更改此选项,因此在该选项上单击,然后将打开 Service Deployment Configuration 界面。


图 11. 服务部署配置
服务部署配置

现在需要把 Web 服务运行时更改为 WSO2 Web Services Application Server,然后单击 OK。这样做应当会返回到 Web Service 界面。


图 12. 选中了 WSAS 运行时的 Web Service 界面
选中了 WSAS 运行时的 Web Service 界面

在此屏幕中,确保选中 Publish the Web service,然后单击 Next。这样做将打开 WSAS Web Service Java Bean Configuration 界面。


图 13. WSAS Web Service Java Bean Configuration
WSAS Web Service Java Bean Configuration

在此屏幕中,确保选中 Generate Default Services.xml 文件,然后单击 Finish。这将导致 WSAS 插件创建 Web 服务必需的所有工件并把它发布到 WSAS 安装中。要检验服务是否已发布,请打开 WSAS Management 控制台。该控制台通常可以在 https://localhost:9443 获得。登录到控制台并转到 Services


图 14. WSAS Management 控制台:服务列表
WSAS Management 控制台:服务列表

您应当会看到 FactorService 列位于部署到 WSAS 中的 Web 服务表。您在列表中看到的其他服务都是 WSAS 附带的默认服务。

祝贺您!您刚刚创建并部署了一项 Web 服务。当然,只是有了一项服务,还不知道它能否正常运行。我们需要测试它。使用 WSAS 将再次简化操作。





回页首


测试 Web 服务

让我们看看创建的 Web 服务。在 Service 表中,我们看到一个 WSDL V2.0 View 链接。单击此链接,然后应当会看到如下所示的 WSDL:


清单 5. FactorService WSDL
                
<wsdl2:description xmlns:wsdl2="http://www.w3.org/ns/wsdl"
     xmlns:tns="http://services.developerworks.org"
     xmlns:wsoap="http://www.w3.org/ns/wsdl/soap"
     xmlns:ns0="http://services.developerworks.org"
     xmlns:ns1="http://org.apache.axis2/xsd"
     xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
     xmlns:wsdlx="http://www.w3.org/ns/wsdl-extensions"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:whttp="http://www.w3.org/ns/wsdl/http"
     xmlns="http://www.w3.org/ns/wsdl"
     targetNamespace="http://services.developerworks.org">
     <wsdl2:documentation>FactorService</wsdl2:documentation>
     <wsdl2:types>
          <xs:schema xmlns:ns="http://services.developerworks.org"
               attributeFormDefault="qualified" elementFormDefault="qualified"
               targetNamespace="http://services.developerworks.org">
               <xs:element name="factor">
                    <xs:complexType>
                         <xs:sequence>
                              <xs:element minOccurs="0" name="num"
                                   type="xs:int" />
                         </xs:sequence>
                    </xs:complexType>
               </xs:element>
               <xs:element name="factorResponse">
                    <xs:complexType>
                         <xs:sequence>
                              <xs:element maxOccurs="unbounded" minOccurs="0"
                                   name="return" type="xs:int" />
                         </xs:sequence>
                    </xs:complexType>
               </xs:element>
          </xs:schema>
     </wsdl2:types>
     <wsdl2:interface name="ServiceInterface">
          <wsdl2:operation name="factor"
               pattern="http://www.w3.org/ns/wsdl/in-out">
               <wsdl2:input element="ns0:factor" wsaw:Action="urn:factor" />
               <wsdl2:output element="ns0:factorResponse"
                    wsaw:Action="urn:factorResponse" />
          </wsdl2:operation>
     </wsdl2:interface>
     <wsdl2:binding name="FactorServiceSOAP11Binding"
          interface="tns:ServiceInterface" type="http://www.w3.org/ns/wsdl/soap"
          wsoap:version="1.1">
          <wsdl2:operation ref="tns:factor" wsoap:action="urn:factor" />
     </wsdl2:binding>
     <wsdl2:binding name="FactorServiceSOAP12Binding"
          interface="tns:ServiceInterface" type="http://www.w3.org/ns/wsdl/soap"
          wsoap:version="1.2">
          <wsdl2:operation ref="tns:factor" wsoap:action="urn:factor" />
     </wsdl2:binding>
     <wsdl2:binding name="FactorServiceHttpBinding"
          interface="tns:ServiceInterface"
          type="http://www.w3.org/ns/wsdl/http">
          <wsdl2:operation ref="tns:factor"
               whttp:location="FactorService/factor" />
     </wsdl2:binding>
     <wsdl2:service name="FactorService"
          interface="tns:ServiceInterface">
          <wsdl2:endpoint name="SecureSOAP11Endpoint"
               binding="tns:FactorServiceSOAP11Binding"
               address="https://192.168.0.104:9443/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a SOAP 11 binding over a HTTPS
               </wsdl2:documentation>
          </wsdl2:endpoint>
          <wsdl2:endpoint name="SecureSOAP12Endpoint"
               binding="tns:FactorServiceSOAP12Binding"
               address="https://192.168.0.104:9443/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a SOAP 12 binding over a HTTPS
               </wsdl2:documentation>
          </wsdl2:endpoint>
          <wsdl2:endpoint name="SecureHTTPEndpoint"
               binding="tns:FactorServiceHttpBinding"
               address="https://192.168.0.104:9443/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a HTTP binding over a HTTPS
               </wsdl2:documentation>
          </wsdl2:endpoint>
          <wsdl2:endpoint name="SOAP11Endpoint"
               binding="tns:FactorServiceSOAP11Binding"
               address="http://192.168.0.104:9762/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a SOAP 11 binding over a HTTP
               </wsdl2:documentation>
          </wsdl2:endpoint>
          <wsdl2:endpoint name="SOAP12Endpoint"
               binding="tns:FactorServiceSOAP12Binding"
               address="http://192.168.0.104:9762/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a SOAP 12 binding over a HTTP
               </wsdl2:documentation>
          </wsdl2:endpoint>
          <wsdl2:endpoint name="HTTPEndpoint"
               binding="tns:FactorServiceHttpBinding"
               address="http://192.168.0.104:9762/services/FactorService">
               <wsdl2:documentation>
                    This endpoint exposes a HTTP binding over a HTTP
               </wsdl2:documentation>
          </wsdl2:endpoint>
     </wsdl2:service>
</wsdl2:description>

在这里应当注意几点。在 wsdl12:types/xs:schema 下,定义了两个类型:factorfactorResponse。这些是服务的输入和输出参数。第一个是单个整数,而另一个是一个无限制的整数序列。如果您必须亲自编写 WSDL,则很可能需要编写这些内容。但是您不必这样做!WSAS 和 WSAS 插件已经帮您完成。

让我们继续探究运行在 WSAS 上的服务。返回到 Service 表并单击 Services 列中的 FactorService 链接。这样做将打开 Service Management 界面。


图 15. Service Management
Service Management

您可以在此屏幕中处理与服务相关的许多事务。您可以管理安全配置和传输配置,但是目前,让我们单击 Try It 链接。这样做将打开 Try Web Service 界面。


图 16. Try Web Service
Try Web Service

继续并输入一些数字,然后做一些测试。这里发生了什么?WSAS 是不是直接调用 Java 类并且只显示结果?是不是调用了某种代理并由它调用您的 Web 服务?观察 HTTP 流量,并且您将看到清单 6 和清单 7 中所示的内容。


清单 6. 调用 FactorService.factor(783) 的 HTTP 请求
                
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
     xmlns:wsa="http://www.w3.org/2005/08/addressing">
     <s:Header>
          <wsa:To>https://localhost:9443/services/FactorService</wsa:To>
          <wsa:ReplyTo>
               <wsa:Address>
                    http://www.w3.org/2005/08/addressing/anonymous
               </wsa:Address>
          </wsa:ReplyTo>
          <wsa:MessageID>
               http://identifiers.wso2.com/messageid/1197868769405/8468659498
          </wsa:MessageID>
          <wsa:Action>urn:factor</wsa:Action>
     </s:Header>
     <s:Body>
          <p:factor xmlns:p="http://services.developerworks.org">
               <num xmlns="http://services.developerworks.org">
                    783
               </num>
          </p:factor>
     </s:Body>
</s:Envelope>

这是 SOAP 请求。它将针对我们在清单 5 中看到的 WSDL 进行验证。您认为我们将从 SOAP 请求得到什么结果?


清单 7. 调用 FactorService.factor(783) 的 HTTP 响应
                
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
     xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
     xmlns:wsa="http://www.w3.org/2005/08/addressing">
     <soapenv:Header>
          <wsa:Action>
               urn:factorResponse
               </wsa:Action>
          <wsa:RelatesTo>
                    http://identifiers.wso2.com/messageid/1197868769405/8468659498
               </wsa:RelatesTo>
     </soapenv:Header>
     <soapenv:Body>
          <ns:factorResponse xmlns:ns="http://services.developerworks.org">
                    <ns:return>1</ns:return>
                    <ns:return>3</ns:return>
                    <ns:return>9</ns:return>
                    <ns:return>27</ns:return>
                    <ns:return>29</ns:return>
                    <ns:return>87</ns:return>
                    <ns:return>261</ns:return>
                    <ns:return>783</ns:return>
          </ns:factorResponse>
     </soapenv:Body>
</soapenv:Envelope>

这是一个 SOAP 响应。同样,这将针对 WSDL 进行验证。WSAS 使用名为 WSRequest(属于 WSAS)的 Ajax 库直接调用了 Web 服务。未使用任何代理。这就是使用您的服务的客户机将使用的请求类型,并且正是 Web 服务将发送回客户机的响应的类型。





回页首


结束语

分享这篇文章……

digg 提交到 Digg
del.icio.us 发布到 del.icio.us

Michael Galpin 从 1998 年就开始进行专业的软件开发。他拥有加州理工学院的数学学位,现在是位于 San Jose, CA 的 eBay 的工程师

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值