Axis与Axis2 java客户端 实例
Axis1 Code:
/**
* @Description Axis1客户端
* @author 无处不在 QQ:381969898
* @DateTime 上午10:13:18
*/
public class Axis1Client {
public String execute() throws Exception {
// 外接口返回值
String result = "";
// 初始化传递的参数
String parmInfo1 = "value1" ;
String parmInfo2 = "value2" ;
// webService参数信息
String webServiceAddress = "";
String webServiceMethod = "";
Service service = new Service();
Call call = (Call) service.createCall();
// 加载WSDL,方法名,nameSpace
webServiceAddress = "http://ip:<port>/xxx/services/xxx?wsdl" ;
webServiceMethod = "method" ;
String nameSpace = "" ;
String soapActionURI = nameSpace + "/" + webServiceMethod;
// 设置wsdl
call.setTargetEndpointAddress(new java.net.URL(webServiceAddress));
// 设置访问的方法名
call.setOperationName(new QName(nameSpace, webServiceMethod));
// 定义参数对象
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
// 设置访问的方法名
call.setOperationName(webServiceMethod);
OperationDesc oper = new OperationDesc();
// String 类型 传递参数
oper.addParameter(new javax.xml.namespace.QName(nameSpace, "parmInfo1"),
new javax.xml.namespace.QName(nameSpace, "string"),
java.lang.String.class,
org.apache.axis.description.ParameterDesc.IN, false, false);
oper.addParameter(new javax.xml.namespace.QName(nameSpace, "parmInfo2"),
new javax.xml.namespace.QName(nameSpace, "string"),
java.lang.String.class,
org.apache.axis.description.ParameterDesc.IN, false, false);
// 返回值类型
oper.setReturnType(XMLType.XSD_STRING);
call.setTimeout(5000);
// 执行
result = (String) call.invoke(new Object[] { parmInfo1 , parmInfo2 });
System.out.println(result);
return result;
}
}
Axis2 客户端:
/**
* @Description Axis2客户端
* @author 无处不在 QQ:381969898
* @DateTime 上午10:14:42
*/
public class Axis2Client {
public String execute() throws Exception {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://ip:<port>/axis2/services/XXX");
options.setTo(targetEPR);
// 指定method方法的参数值
String parmInfo1 = "value1" ;
int parmInfo2 = 12 ;
Object[] opAddEntryArgs = new Object[] {parmInfo1,parmInfo2};
// 指定method方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的method方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://ws.apache.org/axis2", "method");
// 调用method方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
// 下面是调用method2方法的代码,这些代码与调用method方法的代码类似
classes = new Class[] {int.class};
opAddEntry = new QName("http://ws.apache.org/axis2", "method2");
System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
return null;
}
}