使用生成stub的方式调用Web Services
使用生成stub的方式调用Web Services
可以使用wsdl2java命令生成调用Web Services的客户端代码
wsdl2java命令格式:wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p cn.luxh.ws.client -s -o stub
-uri 指定访问的Web Services wsdl文件路径
-p 指定了生成的Java类的包名
-o 指定了生成文件保存的根目录
1)在命令行进入到axis2-1.6.2-bin.zip解压后的\axis2-1.6.2\bin\目录,因为wsdl2java命令在这个目录中
2)输入 wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p client -s -o stub 然后回车
3)执行完后,在\axis2-1.6.2\bin\目录下有个stub文件夹,在stub文件夹下的src\cn\luxh\ws\client包下有个FirstServiceStub.java和FirstServiceUnsupportedEncodingExceptionException.java类,我们把这两个类复制项目中相应的包下,就可以直接调用Web Services方法了。
4)stub客户端代码
package cn.luxh.ws.client;import java.rmi.RemoteException;import javax.xml.namespace.QName;import org.junit.Test;public class FirstClient {@Testpublic void testGetServiceByStub()
throws RemoteException, FirstServiceUnsupportedEncodingExceptionException {FirstServiceStub firstServceStub = new FirstServiceStub();
//wsdl2java命令将Web Services 的方法封装成了静态类
//所以需要通过下面的方法获取返回结果FirstServiceStub.GetInfo getInfo = new FirstServiceStub.GetInfo();
String ruselt = firstServceStub.getInfo(getInfo)。get_return();
System.out.println("getInfo方法返回值:"+ruselt);
FirstServiceStub.SayHello sayHello = new FirstServiceStub.SayHello();
sayHello.setName("LiHuai");ruselt = firstServceStub.sayHello(sayHello)。get_return();
System.out.println("sayHello方法返回值:"+ruselt);
}

浙公网安备 33010602011771号