标签:web service wsdl java eclipse webservice
步骤1:下载Axis2
选择Standard Binary Distribution的zip包;解压到任一路径下
步骤2:配置Axis2
Window -> Preferences -> Web Service
步骤3:开发WebService
1)、新建一个Dynamic Web Project,命名为“WebService”
2)、新建一个class,命名为“CalculateService”
package com;
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;
}
}右键“WebService”项目-> new -> other
启动后,点击next -> next;都默认即可;完成结果如下:
4)、测试web服务
在浏览器中输入上步中web service exploer的地址:
步骤4:开发客户端调用程序
1)、新建Java Project项目
2)、把Axis2安装目录下lib目录下的所有jar包导入到该工程中
3)、新建CalculateServiceTest类
package webServiceTest;
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 CalculateServiceTest {
public static void main(String[] args) throws AxisFault {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/first/services/CalculateService");
options.setTo(targetEPR);
<span style="color:#ff6666;">// 指定要调用的计算机器中的方法及WSDL文件的命名空间(其实就是http://调用方法所在类的所在包的名字):com</span>
QName opAddEntry = new QName("http://com", "plus");// 加法
QName opAddEntryminus = new QName("http://com", "minus");// 减法
QName opAddEntrymultiply = new QName("http://com", "multiply");// 乘法
QName opAddEntrydivide = new QName("http://com", "divide");// 除法
// 指定plus方法的参数值为两个,分别是加数和被加数
Object[] opAddEntryArgs = new Object[] { 1, 2 };
// 指定plus方法返回值的数据类型的Class对象
Class[] classes = new Class[] { float.class };
// 调用plus方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntryminus,
opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,
opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,
opAddEntryArgs, classes)[0]);
}
}
WebService-----Axis2搭建WebService项目
标签:web service wsdl java eclipse webservice
原文地址:http://blog.csdn.net/hekewangzi/article/details/40754593