package edu.sjtu.webservice;
/**
* 计算器运算
* @author rongxinhua
*/
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;
}
}
package edu.sjtu.webservice.test;
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 {
/**
* @param args
* @throws AxisFault
*/
public static void main(String[] args) throws AxisFault {
// TODO Auto-generated method stub
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/WebServiceTest1/services/CalculateService");
options.setTo(targetEPR);
// 指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法
QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//减法
QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法
QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","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]);
}
}
package edu.sjtu.webservice;
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 + " 已更新。");
}
}
HelloServiceTest2.java
package edu.sjtu.webservice.test;
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 HelloServiceTest2 {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public HelloServiceTest2(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}
public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
// Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws AxisFault,
ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService";
final String targetNamespace = "http://webservice.sjtu.edu";
HelloServiceTest2 client = new HelloServiceTest2(endPointReference);
String opName = "sayHelloToPersonNew";
Object[] opArgs = new Object[] { "My Friends" };
Class<?>[] opReturnType = new Class[] { String[].class };
Object[] response = client.invokeOp(targetNamespace, opName, opArgs,
opReturnType);
System.out.println(((String[]) response[0])[0]);
}
}
HelloServiceTest.java
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 HelloServiceTest {
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/WebServiceTest1/services/HelloService");
options.setTo(targetEPR);
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew");
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[] { "xuwei" };
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有