java+axis2-1.6.2 webservice接口开发
开发工具:myeclipse8.6+axis2-1.6.2+tomcat6.0
一、插件安装
myeclipse和tomcat没有安装axis2的需要下载axis2所需架包和axis-eclipse-plugin插件
1.myeclipse安装axis2插件
axis2-eclipse-codegen-plugin-1.6.2.zip
axis2-eclipse-service-plugin-1.6.2.zip
将上面两个解压,找到myeclipse的安装路径,D:\Program Files\Genuitec\MyEclipse-8.6\dropins
将上面解压出来的plugins文件夹放到dropins文件夹下,然后新建文件axis2.link
文件内容 path=D:\\Program Files\\Genuitec\\MyEclipse-8.6\\dropins\\plugins
重启myeclipse,在myeclipse中new --->other输入axis,如果有Axis2 Wizards文件和下面两个东西,说明安装成功了
2.tomcat安装axis2插件
将下载的axis2架包axis2-1.6.2-war.zip解压,将axis2.war文件放到tomcat安装路径D:\tomcat-6.0.30\webapps下面
启动tomcat,在webapps下面就会多一个axis2文件,然后再浏览器上输入http://localhost:8080/axis2
能看到axis2 - Home页面,即安装成功.
二、服务器发布与创建
创建一个java项目,编写一个类
package com.webservice;
public class TestService {
public String sayHello(String name){
System.out.println(name+"在调用sayHello...");
if(name==null || "".equals(name)){
return "请输入name.";
}
return "hello,"+name;
}
} 1.
对该项目进行打包
在该项目上右击,选择export,弹出
next
点击Finsh
然后再刚才保存的位置,会出现一个jar包
2.打包arr
在该项目上右击,new-->other-->Axis2 Wizards-->Axis2 Service Archiver
next
next
选择skip wsdl next
点击Browse,选择上面打的jar包,选中点击Add,然后next
勾选那个复选框,点击next
service name:输入一个名称
Class name :输入上面创建的类的路径,然后点击load,下面就会显示此类中的方法,然后点击next
Finish完成
你会看到刚才保存的路径下多了一个arr文件,将此文件放到tomcat安装Axis2的下面
D:\tomcat-6.0.30\webapps\axis2\WEB-INF\services
重启tomcat,在浏览器上输入http://localhost:8088/axis2/services/TestService?wsdl 会看到以下页面
此服务端就算完成了。
三、客户端
新建一个java web项目,右击此项目,new-->other-->Axis2 Wizards-->Axis2 Code Genertor
next
next
next
Finish,然后刷新项目,你会发现项目中多出了一些类,但项目报错,这是需要把
axis2-1.6.2-bin.zip 解压出来,把lib文件夹下面所有架包放到项目WEB-INF的lib下面,项目就没错了
然后创建一个测试类
package com.client;
import com.webservice.SayHello;
import com.webservice.TestServiceStub;
public class TestClient {
public static void main(String[] args) {
try{
TestServiceStub tss=new TestServiceStub();
SayHello say=new SayHello();
say.setName("xxx");
String result=tss.sayHello(say).get_return();
System.out.println(result);
}catch(Exception e){
}
}
}运行tomcat,然后运行测试类,控制台就会输出
hello,xxx
整个webservice就写完了。
如有问题或有更好方法欢迎讨论。