文章作为学习笔记和分享用。
准备工作:下载安装eclipse和axis2-1.5.4-bin.zip(最新版本的搭建有问题就选择了此版本,下载本地找一个目录解压)
1.指定axis2路径:Window->Preferences->Web Services->Axis2 Preferences,在Axis2 Runtime界面点击Browse…选择Axis2 解压包路径。
2.新建一个web工程WebServiceTest1(如果工程异常可以选择创建2.5而非3.0),在工程中加如下类
public class CalculateService {
//加法
public float plus(float x, float y) {
return x + y;
}
//减法
public float minus(float x, float y) {
return x - y;
}
//乘法
public float multiply(float x, float y) {
return x * y;
}
//除法
public float divide(float x, float y) {
if(y!=0)
{
return x / y;
}
else
return -1;
}
}
3.在WebServiceTest1工程名上右击new ->other->Web services->Web service->Next出现如下界面。在Web service type栏,Service implementation点击Browse…搜索并选择上一步添加的类CalculateService。在Configuration下点击serverRuntime选择tomcat(我用的是tomcat7.0),点击Web service runtime选择Apache Axis2点击“确认”。在Client type界面Configuration下点击serverRuntime选择tomcat,点击Web service runtime选择Apache Axis2点击“确认”。将图标上拉至”Test client”。配置好的界面如下图。点击“下一步”。
如下图选择Generate a default services.xml file
如下图点击Start server。剩余默认点击下一步直至完成。
4.完成之后会出现如下网页,左侧是方法名,可以点击方法名进行测试。
5.在Client添加测试类Test
package webs_test;
import java.rmi.RemoteException;
public class Test {
public static void main(String[] args) {
CalculateServiceProxy proxy = new CalculateServiceProxy();
try {
float l = proxy.divide(99, 3);
System.out.println("divide(99, 3) = "+l);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
运行结果为:divide(99, 3) = 33.0