Axis2 的安装配置以及简单例子
一.安装配置
1. axis2 的主页 http://ws.apache.org/axis2/index.html
axis2 1.0 的下载地址 http://ws.apache.org/axis2/download/1_0/download.cgi
2. servlet 容器安装 (以tomcat5.5 为例)
从上面给出的下载地址下载 War Distribution (axis2.war)
放到 tomcat\webapps 目录下,在浏览器中输入 http://localhost:8080/axis2 正常显示Welcome 的页面,(查看tomcat\webapps 可以看到生成一个名为axis2的文件夹) 点击 Validate 验证安装是否正常
一切正常侯即可使用axis2发布web服务
axis2 web管理环境的用户名:admin 密码:axis2
3. 配置开发相关环境变量
需要添加 AXIS2_HOME = axis2解压目录
为了随处可以使用axis2 提供的代码生成工具,可以在path 环境变量里添加 %AXIS2_HOME%\bin
二.开发服务
axis2 通过两种方式来开发一个web服务,一种方式通过使用axis2 primary APIs
另一种方式使用wsdl文件来生成web服务的客户端和服务端框架代码
A. 通过 axis2 primary APIs 开发web service
这种方式需要学习axis2 对象模型(叫AXIOM)的基础知识 ,而且可扩展性不如后一种方式。
B. 通过wsdl文件开发和测试一个web服务
AXIS2_HOME\samples\wsdl下有wsdl文件的例子 ,以Axis2SampleDocLit.wsdl 为例,简单介绍一下生成框架代码的过程,
1. 在命令行模式里,进入到AXIS2_HOME\samples\wsdl 目录
注意:这个Axis2SampleDocLit.wsdl 文件有点问题,需要将里边 style = "rpc"的地方全部改成 style = "document"
2. 运行 wsdl2java -uri .\Axis2SampleDocLit.wsdl -ss -s d -d xmlbeans -g -o Axis2SampleDocLit -p org.apache.axis2.userguide
这个命令在当前目录生成一个文件夹Axis2SampleDocLit
3. 添加服务: 编辑
Axis2SampleDocLit\src\org\apache\axis2\userguide\Axis2SampleDocLitServiceSkeleton.java 文件
编辑 echoString 方法为
public org.apache.axis2.userguide.xsd.EchoStringReturnDocument echoString(
org.apache.axis2.userguide.xsd.EchoStringParamDocument param4)
{
String name = param4.getEchoStringParam();
String retStr = "Hello " + name + "!";
EchoStringReturnDocument esrd = EchoStringReturnDocument.Factory.newInstance();
esrd.setEchoStringReturn(retStr);
return esrd;
}
4 . 进入目录Axis2SampleDocLit,可以看到build.xml,运行ant(ant的安装相当简单 ,下载ant 1.6.5 将 "ant解压目录\bin" 添加到path 环境变量即可)
ant 会告诉你build 成功或者失败 ,
5. build 成功后转到浏览器,进入 http://127.0.0.1:8080/axis2/axis2-admin/ 登陆,Upload Service ,选择
Axis2SampleDocLit\build\lib\Axis2SampleDocLitService.aar,
然后upload ,服务就发布成功了
6. 客户端代码
public static void main(String[] args) {
try {
java.lang.String str = new java.lang.String();
Axis2SampleDocLitServiceStub stub= new Axis2SampleDocLitServiceStub
("http://localhost:8080/axis2/services/Axis2SampleDocLitService");
//Create the request document to be sent.
EchoStringParamDocument reqDoc= EchoStringParamDocument.Factory.newInstance();
reqDoc.setEchoStringParam("Your name");
//invokes the Web service.
EchoStringReturnDocument resDoc = stub.echoString(reqDoc);
System.out.println(resDoc.getEchoStringReturn());
} catch (Exception e) {
e.printStackTrace();
}
}
编译运行,应该可以看到hello Your name 输出
C. 可以通过axis1.x 介绍 C.6.b的介绍 通过java 代码生成wsdl的原型文件
D. 也可以通过Axis2_Codegen_Wizard_1.2.0 eclipse 插件 来开发wsdl 和 框架文件
然后通过Axis_Service_Archiver_1.0.0 eclipse 插件 打包服务
这两个插件的下载地址
http://www.apache.org/dyn/mirrors/mirrors.cgi/ws/axis2/tools/1_0/Axis2_Service_Archiver.zip
http://www.apache.org/dyn/mirrors/mirrors.cgi/ws/axis2/tools/1_0/Axis2_Code_Generator.zip
以Axis2_Codegen_Wizard_1.2.0 为例:
下载后解压 ,解压后的文件夹是一个eclipse 插件工程,导入eclipse 开发环境 , 然后在package explorer 视图中 右键 export
然后选择 deployable plug-ins and .... 然后指定输出目录,这样在输出目录将会生成 plugins 文件夹 ,将该文件夹拷贝到 eclipse 安装目录
重启eclipse ,在new -->other 的列表里应该会出现axis2 wizard 的 项 ,说明安装插件成功,工具的使用请查看
"Axis2_Codegen_Wizard_1.2.0解压目录"\help\wsdl2java-plugin.html