写好一个service服务后,点击右键,出现Web Service。。。如下图:
下面的步骤,除了启动server tomcat 服务外,其它都点击next就行了。
web hello service源代码:
package com.test.service;
public class HelloService {
public String sayHelloNew() {
return "hello";
}
public String sayHelloToPersonNew(String name) {
if (name == null) {
name = "nobody";
}
return "hello," + name;
}
public void updateData(String data) {
System.out.println(data + " 已更新。");
}
}
client:即调用web service的服务。
package com.test.client;
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 CalculateClient {
public void invokeHelloService() throws AxisFault {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/CalculateService/services/HelloService");
options.setTo(targetEPR);
// 指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
QName opAddEntry = new QName("http://service.test.com",
"sayHelloToPersonNew");// 加法
// 指定plus方法的参数值为两个,分别是加数和被加数
Object[] opAddEntryArgs = new Object[] { "优快云" };
// 指定plus方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 调用plus方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
}
public void invokeCalculateService() throws AxisFault {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/CalculateService/services/CalculateService");
options.setTo(targetEPR);
// 指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
QName opAddEntry = new QName("http://service.test.com", "plus");// 加法
// 指定plus方法的参数值为两个,分别是加数和被加数
Object[] opAddEntryArgs = new Object[] { 1, 5 };
// 指定plus方法返回值的数据类型的Class对象
Class[] classes = new Class[] { Float.class };
// 调用plus方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
}
public static void main(String args[]) throws AxisFault {
CalculateClient client = new CalculateClient();
client.invokeCalculateService();
client.invokeHelloService();
}
}
2013-11-15 15:24:50 org.apache.axis2.deployment.ModuleDeployer deploy
信息: Deploying module: addressing-1.6.2 - file:/E:/repository/org/apache/axis2/axis2/1.6.2/axis2-1.6.2.jar
6.0
2013-11-15 15:24:51 org.apache.axis2.deployment.ModuleDeployer deploy
信息: Deploying module: addressing-1.6.2 - file:/E:/repository/org/apache/axis2/axis2/1.6.2/axis2-1.6.2.jar
hello,优快云
参考资料:
http://blog.youkuaiyun.com/xw13106209/article/details/7049614