1、下载axis2所需的jar包放入WEB-INF/lib下
jar包下载地址:https://download.csdn.net/download/qq_43072912/10784322
2、创建一个Java Project项目
项目结构:
3、test.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 test {
public static void main(String[] args) {
try {
//本机tomcat端口默认为8081,参数是wsdl网址的一部分
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/Testservice");
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(2*20000L);//超时时间20s
options.setTo(targetEPR);
QName qname = new QName("http://test.yaya.com", "test");
//第一个值:类地址反写 第二个值:方法名
String str = "李四"; //方法的入参
Object[] param = new Object[]{str};
Class<?>[] types = new Class[]{String.class}; //这是针对返值类型的
/**
* RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
* invokeBlocking方法有三个参数
* 第一个参数的类型是QName对象,表示要调用的方法名;
* 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
* 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
*
* 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
*/
Object[] response1 = sender.invokeBlocking(qname, param, types);
System.out.println(response1[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里客户端就结束了,如需服务端的参考上篇文章:https://blog.csdn.net/qq_43072912/article/details/84067142