仿照搜索到的方法
服务端 WSDLTest.java
/**
* 提供了一个说Hello的服务
* @return
*/
public String sayHello(String name){
return "Hello "+name;
}
/**
* 提供了一个做加法的服务
* @param a
* @param b
* @return
*/
public int add(int a,int b){
return a + b;
}
客户端程序
Axis2WBClient.java
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;
public class Axis2WBClient {
public static void main(String[] args) {
String xmlStr = "xiao.liu";
String url = "http://localhost:8080/WSDL_Test/services/uimWBService";
String method = "sayHello";
Axis2WBClient.sendService(xmlStr, url, method);
}
@Test
public void testOne() {
try {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/WSDL_Test/services/uimWBService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[] { 1,100 };
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://wsdl.founder.com",
"add");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
} catch (Exception e) {
// TODO: handle exception
}
}
public static String sendService(String xmlStr, String url, String method) {
String xml = null;
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
QName opAddEntry = new QName("http://wsdl.founder.com", method);
// 参数,如果有多个,继续往后面增加即可,不用指定参数的名称
Object[] opAddEntryArgs = new Object[] { xmlStr };
// 返回参数类型,这个和axis1有点区别
// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
Class[] classes = new Class[] { String.class };
xml = (String) serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0];
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(xml);
return xml;
}
}