使用Eclipse+Axis2构建Web Service应用(转)
本文介绍了如何使用tomcat和Axis2开发、部署和使用Web Service,并且开发了一个简单的例子,通过例子开发流程来向大家展示如何创建Axis2的服务端和客户端。该例子与其他例子最大的不同是不是用客户端stub存根方式调用web服务,提供了一种统一的客户端调用方式。希望读者读完本文后能够快速上手Axis2的应用开发。
一、开发环境和软件下载
开发环境是:Eclipse3.4 + JDK1.6 + Tomcat6.0 + Axis2 1.3。这些软件都是各个软件最新版本,下载方式由于软件网站的更新,地址也会更新,这里就不再贴出来了,就请读者到相应网站下载。
软件下载和安装:
(1) JDK1.6环境变量配置:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_07,其中C:\Program Files\Java是我的JDK安装目录。
Path: %JAVA_HOME%\bin
Classpath: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
(2) tomcat6.0环境变量配置:
CATALINA_HOME:C:\Program Files\Apache Software Foundation\Tomcat 6.0, 其中C:\Program Files\Apache Software Foundation是我的Tomcat 6.0安装目录。
其实这里tomcat6.0不需要配置环境变量,因为最新版本不需要配置。
(3) Axis2 1.3下载:从[url]http://apache.justdn.org/ws/axis2/1_4_1/[/url]网站上下载axis2-1.3-war.zip 。将其解压,把其中的axis2.war文件复制到tomcat的webapps目录下即可完成Axis2的安装。
安装完成后,启动tomcate,在地址栏内输入[url]http://localhost:8080/axis2/[/url],打开如下图界面,表明安装正确:
(4) Eclipse3.4下载:到[url]www.eclipse.org[/url]官方网站下载,直接解压后配置Windows/Preference中的JDK即可。
(5) 下载Eclipse Axis2插件:axis2-eclipse-codegen-wizard-1.4.zip和axis2-eclipse-codegen-wizard-1.4.zip。下载地址是:[url]http://apache.justdn.org/ws/axis2/tools/1_4_1/ [/url] 这两个插件解压到Eclipse安装目录下plugins中即可。打开Eclipse,选择File/New/Other菜单项,看到如下界面表明安装成功:
安装这两个插件的目的是:方便生成Axis2服务和生成Axis2客户端,这里我使用了无Stub方式的客户端调用服务,因此就不演示生成Axis2客户端的功能了。
二、构建服务
(1)在 Eclispse 中添加一个用户库命名为 axis2 ,将 axis2\lib 下的包均添加进来。这一步操作的目的是为了以后工程添加 axis2 所需的 jar 文件方便。
(2)
<!-- [endif]-->
建立一个 JavaProject 命名为 ws ,将 axis2 用户库加入到 build path 中。
(3) 现在开始编写要发布的 WebSevice ,在 src 中建包 briup ,建立 Hello 类如下:
public class Hello {
public String sayHello(String user) {
return "Hello, " + user;
}
}
2.发布Web Service
打包要发布的 Service , Eclipse 中 New --> File --> Other --> Axis2 wizards --> Axis2 Services Archiver ,按照向导选择刚建立的类编译后的 class 文件。
(1)选择 class 文件目录,注意,不是 java 源文件,而是 classes 目录,这里需要注意由于你的类是带包briup的,因此不要选到包这个目录。
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class WsClient {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public WsClient(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}
public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
//Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws AxisFault, ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference = "http://localhost:8080/axis2/services/ws";
final String targetNamespace = "http://briup";
WsClient client = new WsClient(endPointReference);
String opName = "sayHello";
Object[] opArgs = new Object[]{"Repace中心"};
Class<?>[] opReturnType = new Class[]{String[].class};
Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
System.out.println(((String[])response[0])[0]);
}
}