Eclipse Apache Axis2集成 一. 安装Eclipse 详细见《Web Service Eclipse安装》文档,将在近期更新
二. 安装Apache Axis2详细见《Apache Axis2安装》 http://bldmickey.blog.sohu.com/56486087.html
三. 开发环境描述软件 | 版本 | Eclipse SDK Version | 3.3.0 | STP(SOA Tools Platform) | 0.6.0 | WST(Web Standard Tools) | 2.0.0 | DTP(Data Tools Platform) | 1.5.0 | JST(J2EE Standard Tools) | 2.0.0 | Apache Axis2 | 1.3 | Apache Tomcat
| 5.5.20
|
四. 下载Apache Axis2 Tools- Code Generator Wizard - Eclipse Plug-in 版本1.3
包括Java2WSDL和WSDL2Java。其中Java2WSDL从java class开始创建WSDL文件;WSDL2Java从WSDL文件开始创建java class。下载地址:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_3/axis2-eclipse-codegen-wizard.zip
- Service Archive Wizard - Eclipse Plug-in版本1.3
用来创建web服务的软件包(aar文件或者jar文件)。下载地址:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_3/axis2-eclipse-service-archiver-wizard.zip
五. Apache Axis2 Tools的安装- 解压下载的文件
- 拷贝解压的内容到eclipse安装目录下的plugins目录下
六. Eclipse+Axis2集成例子6.1 Eclipse Axis2参数设置- 菜单选择:Window->Preference…->Web Service->Axis2 Preferences
- 设置Axis Runtime Location,也就是Axis2 Runtime安装的位置,例如:C:\Program Files\axis2
6.2 创建Web Project- 菜单选择:File->New Project…->Web(Dynamic Web Project)选择Next, 在项目名称中输入SayHi
6.3 添加Axis2 Library在项目属性中->Java Build Path->Library中添加一个自定义的Axis2_Library的库,将axis2 web应用WEB-INF/lib目录中所有jar添加到新定义的库中。添加后项目的情况: 6.4 创建WSDL文件- 菜单选择:File->New Other…->Web Service WSDL->next
- 选中指定路径SayHi/src,指定WSDL的文件名:SayHi.wsdl,选择Next
- Target namespace:修改成http://www.wsexample.org/SayHi,选择Finish
- 在WSDL design的tab中修改WSDL的设计。例如修改:Operation的名称为:SayHiOper,具体有关WSDL的信息请查看《Web Service WSDL.doc》和《Web Service XML.doc》
<wsdl:types> <xsd:schema targetNamespace="http://www.example.org/SayHi/"> <xsd:element name="SayHiOper"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SayHiOperResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types>
|
6.5从WSDL文件生成Java Server代码- 选择File->New->Other,从对话框中选取Axis2 Wizards->Axis2 Code Generator,选择Next
- 选择“Generate java source code from WSDL file”,点击“Next”进入下一步
- 选择WSDL文件所在的路径:选中SayHi.wsdl文件
- Axis2 Codegen Wizard对话框中选择:(完成后选择Next继续)
- Codegen Option为custom
- Databinding Name为缺省的ADB(还有xmlbeans、jibx)
- 先创建服务器端代码
- 确定java服务器端程序的输出位置,例如:SayHi\(不包括src目录)。生成的代码输出的位置。注意:在程序的目录中,不需要指定到src目录下。
- 项目程序结果:
6.6 修改Service代码在SayHiSkeleton.java程序中添加处理代码(蓝色部分),实现了SayHiOper函数接口,获取web请求“sayHiOper.getIn() ”,然后添加相关问候信息“"Hi," request ".How are you? ”
package org.example.www.sayhi; public class SayHiSkeleton{ { try { SayHiOperResponse response = new SayHiOperResponse(); response.setOut("Hi,"+sayHiOper.getIn()+". How are you?"); return response; } catch(UnsupportedOperationException e) {throw e;} //throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#SayHiOper"); } } |
6.7 创建发布Package- 菜单选择:File->New->Other…->Axis2 Wizards中选择Axis2 Service Archive,选择Next
- 选择classes的路径,例如:SayHi\build\classes,选择Next
- 选择WSDL文件,选择Next,在选择Next
- 设置Service XML文件,例如:SayHi\resources\services.xml
- 设置输出的文件和目录,例如tomcat下的部署目录:webapps\axis2\WEB-INF\services,输出文件名:SayHi.aar,直接可以输出到tomcat对应的axis2 services的目录中。
6.8 浏览器测试
<ns1:SayHiOperResponse xmlns:ns1=”http://www.example.org/SayHi/"> <out>Hi,bldmickey. How are you?</out> </ns1:SayHiOperResponse> | 其中SayHiOper是对应的Web Service的Operation,in对应的是函数对应的输入值。详细对应在wsdl文件中。输入参数的值是:bldmickey,输出的结果是:Hi,bldmickey. How are you?
6.9 从WSDL生成Java Client代码- 选择File->New->Other,从对话框中选取Axis2 Wizards->Axis2 Code Generator,选择Next
- 具体的操作和6.5中创建Server端代码类似,只是选择输出客户端代码,具体见下图:
- 确定java客户端程序的输出位置,例如:SayHi
- 增加JUNIT库(测试代码需要JUNIT库)
- 项目属性->Java Build Path->Libraries中Add Library…->选择JUnit, Next继续,缺省选择JUnit3完成
- 创建的文件包括:SayHi\src\org\example\www\sayhi\SayHiStub.java(Stub程序)和SayHi\test\org\example\www\sayhi\SayHiTest.java(测试程序)
6.10 修改客户端代码和测试- 修改test代码的路径问题,将SayHiTest.java从test\org\example\www\sayhi目录移动到src\org\example\www\sayhi\目录
- 修改代码
package org.example.www.sayhi;
public class SayHiTest extends junit.framework.TestCase{
public void testSayHiOper() throws java.lang.Exception{
String url="http://localhost:8081/axis2/services/SayHi";
org.example.www.sayhi.SayHiStub stub =
new org.example.www.sayhi.SayHiStub(url);
//org.example.www.sayhi.SayHiStub stub =
// new org.example.www.sayhi.SayHiStub();
org.example.www.sayhi.SayHiStub.SayHiOper sayHiOper2=
(org.example.www.sayhi.SayHiStub.SayHiOper)getTestObject(org.example.www.sayhi.SayHiStub.SayHiOper.class);
sayHiOper2.setIn("Client bldmickey");
System.out.println(stub.SayHiOper(sayHiOper2).getOut());
assertNotNull(stub.SayHiOper(sayHiOper2));
// assertNotNull(stub.SayHiOper(sayHiOper2));
}
//Create an ADBBean and provide it as the test object
public org.apache.axis2.databinding.ADBBean getTestObject(java.lang.Class type) throws Exception{
return (org.apache.axis2.databinding.ADBBean) type.newInstance();
}
}
}
|
- Console中输出:Hi,Client bldmickey. How are you?
6.11 TCP Monitor监控Web Service运行tcpmon.bat
添加监听8888端口,定向到127.0.0.1端口8081
浏览器中访问http://localhost:8888/axis2/services/SayHi/SayHiOper?in=bldmickey
输入:
GET /axis2/services/SayHi/SayHiOper?in=bldmickey HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-us
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727)
Host: 127.0.0.1:8888
Connection: Keep-Alive
| 输出
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 15 Aug 2007 06:03:12 GMT
7e
<ns1:SayHiOperResponse xmlns:ns1="http://www.example.org/SayHi/"><out>Hi,bldmickey. How are you?</out></ns1:SayHiOperResponse>
0
|
6.12 代码分析Axis2的新架构,写一个服务,会有很多种不同的方法,可以用pojo结合rpc模式来写,也可以用Axis2自己的Axiom API从底层写,也可以从WSDL生成相应框架,然后填写相应逻辑。
对于WSDL生成框架代码这种形式,又有不同的数据绑定可以使用。Axis2.1.1 可以使用adb(axis2自己的data binding)、xmlbeans、jibx、jaxme、jaxbri。其中常用的还是前两种,axis2官方是极力推荐自己的data binding的,因为速度快,架构相对简单,而且有一些独有的特性。xmlbeans,是对模式支持最好的data binding,一些特别复杂的模式只能在xmlbeans下才能正确生成框架代码。其他几种代码wsdl2java目前都不是支持得很好,都还在实验改进阶段。
面对这么多数据绑定模式,很多人都无从选择。官方maillist上也有好多人问该用哪个。查了一些资料,据说,adb很好用,而且速度快,如果只是简单的模式,那么使用这种数据绑定就可以了。xmlbeans的强力模式支持,可以支持足够负责的模式,速度上也只是比adb低5%左右。而jibx很多人没用过,因为使用没有前两种方便,不过jibx支持unwrapping代码生成(普通代码生成的服务方法,一般只有一个参数,而unwrapping可以生成多个参数的方法,相当直观。) |