第一,RPC方式,不生成客户端代码,引入相应的axis2的jar包(不好用)
注意:暂时没有成功调用,没有参数传递时远程调用成功,当有参数传递时远程调用失败;

package com.ming.axis2;import javax.xml.namespace.QName;import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;/*** 方法一: 应用rpc的方式调用 这种方式就等于远程调用, 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。* 使用org.apache.axis2.rpc.client.RPCServiceClient类调用WebService;* * 【注】:* 如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数* 第一个参数的类型是QName对象,表示要调用的方法名;* 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[],* 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{};* 第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。* * 如果被调用的WebService方法没有返回值 应使用 invokeRobust方法,* 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。* */
public class MobileClientRPC {/*** * @Title: testRPCClient* @Description: TODO(rpc远程调用,失败,无法正确传参)* @return void    返回类型*/@Testpublic void testRPCClient() {try {// 使用RPC方式调用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();// 创建WSDL的URL,注意不是服务地址String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";// 指定调用WebService的URLEndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();// 确定目标服务地址options.setTo(targetEPR);// 确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)options.setAction("http://WebXml.com.cn/getMobileCodeInfo");// 指定方法的参数值Object[] parameters = new Object[] {"1866666666", ""};// 创建服务名称// 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace)// 2.localPart - 服务视图名 (wsdl文档中operation的方法名称,例如<wsdl:operation name="getMobileCodeInfo">)QName qname = new QName("http://WebXml.com.cn/", "getMobileCodeInfo");// 调用方法一 传递参数,调用服务,获取服务返回结果集OMElement element = serviceClient.invokeBlocking(qname, parameters);System.out.println(element);/** 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。* 我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果*/String result = element.getFirstElement().getText();System.out.println(result);// 调用方法二 getPrice方法并输出该方法的返回值// 指定方法返回值的数据类型的Class对象Class[] returnTypes = new Class[] {String.class};Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);String r = (String) response[0];System.out.println(r);} catch (AxisFault e) {e.printStackTrace();}}/*** * @Title: Weather* @Description: TODO(rpc远程调用,成功,没有参数能够成功)* @return void    返回类型* @throws AxisFault*/@Testpublic void Weather() throws AxisFault{    //使用RPC方式调用WebService         RPCServiceClient serviceClient = new RPCServiceClient();    Options options = serviceClient.getOptions();    //指定调用WebService的URL    EndpointReference targetEPR = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");    options.setTo(targetEPR);options.setAction("http://WebXml.com.cn/getRegionProvince");//指定方法的参数值    Object[] opAddEntryArgs = new Object[] {};    //指定要调用的方法及WSDL文件的命名空间    QName opAddEntry = new QName("http://WebXml.com.cn/", "getRegionProvince");    //调用法并输出该方法的返回值    System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));  }/*** * @Title: Weather1* @Description: TODO(rpc远程调用,失败,单个参数失败)* @return void    返回类型*/@Testpublic void Weather1() {    try {//使用RPC方式调用WebService         RPCServiceClient serviceClient = new RPCServiceClient();    Options options = serviceClient.getOptions();    //指定调用WebService的URL    EndpointReference targetEPR = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");    options.setTo(targetEPR);options.setAction("http://WebXml.com.cn/getSupportCityDataset");//指定方法的参数值    Object[] opAddEntryArgs = new Object[] {"北京"};   //指定要调用的方法及WSDL文件的命名空间    QName opAddEntry = new QName("http://WebXml.com.cn/", "getSupportCityDataset");    //调用法并输出该方法的返回值    System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));} catch (AxisFault e) {e.printStackTrace();}  }
}

第二,应用document方式调用 用ducument方式应用现对繁琐而灵活,引入相应的axis2的jar包。现在用的比较多。
即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端(推荐使用)

package com.ming.axis2;import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.junit.Test;/***
* @ClassName: MobileClientDoc
* @Description: TODO
* 方法二: 应用document方式调用 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
* 即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端
*
* @date 2017年11月9日 下午1:27:17
**/
public class MobileClientDoc {/*** * @Title: Weather* @Description: TODO(document远程调用,成功,没有参数能够成功)* @return void    返回类型* @throws AxisFault*/@Testpublic void Weather() throws AxisFault{ServiceClient serviceClient = new ServiceClient();//创建服务地址WebService的URL,注意不是WSDL的URLString url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";EndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();options.setTo(targetEPR);//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)options.setAction("http://WebXml.com.cn/getRegionProvince");OMFactory fac = OMAbstractFactory.getOMFactory();/** 指定命名空间,参数:* uri--即为wsdl文档的targetNamespace,命名空间* perfix--可不填*/OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");// 指定方法OMElement method = fac.createOMElement("getSupportCityDataset", omNs);method.build();//远程调用web服务OMElement result = serviceClient.sendReceive(method);System.out.println(result);}/*** * @Title: Weather1* @Description: TODO(document远程调用,成功,没有参数能够成功)* @return void    返回类型*/@Testpublic void Weather1() {try {ServiceClient serviceClient = new ServiceClient();//创建服务地址WebService的URL,注意不是WSDL的URLString url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";EndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();options.setTo(targetEPR);//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)options.setAction("http://WebXml.com.cn/getSupportCityDataset");OMFactory fac = OMAbstractFactory.getOMFactory();/** 指定命名空间,参数:* uri--即为wsdl文档的targetNamespace,命名空间* perfix--可不填*/OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");// 指定方法OMElement method = fac.createOMElement("getSupportCityDataset", omNs);// 指定方法的参数OMElement theRegionCode = fac.createOMElement("theRegionCode", omNs);theRegionCode.setText("北京");method.addChild(theRegionCode);method.build();//远程调用web服务OMElement result = serviceClient.sendReceive(method);System.out.println(result);} catch (AxisFault axisFault) {axisFault.printStackTrace();}}/*** * @Title: Weather1* @Description: TODO(document远程调用,成功,多个参数)* @return void    返回类型*/@Testpublic void MobileCodeWS() {try {ServiceClient serviceClient = new ServiceClient();//创建服务地址WebService的URL,注意不是WSDL的URLString url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";EndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();options.setTo(targetEPR);//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)options.setAction("http://WebXml.com.cn/getMobileCodeInfo");OMFactory fac = OMAbstractFactory.getOMFactory();/** 指定命名空间,参数:* uri--即为wsdl文档的targetNamespace,命名空间* perfix--可不填*/OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");// 指定方法OMElement method = fac.createOMElement("getMobileCodeInfo", omNs);// 指定方法的参数OMElement mobileCode = fac.createOMElement("mobileCode", omNs);mobileCode.setText("15932582632");OMElement userID = fac.createOMElement("userID", omNs);userID.setText("");method.addChild(mobileCode);method.addChild(userID);method.build();//远程调用web服务OMElement result = serviceClient.sendReceive(method);System.out.println(result);} catch (AxisFault axisFault) {axisFault.printStackTrace();}}
}

第三,用wsdl2java工具,生成客户端方式调用webservice服务
步骤:
(1)引入axis2的相应jar包;
(2)下载axis2-1.7.4-bin.zip,在bin目录中有wsdl2java,用于根据WSDL生成相应的服务端和客户端代码的生成工具;

wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。
命令行格式为:WSDL2Java [options] -uri <url or path> : A url or path to a WSDL
例如:
wsdl2java -uri http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl -s -p com.ming.mobile -o build\clientwsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o build\client其中常用的options具体如下:
-o <path> : 指定生成代码的输出路径
-a : 生成异步模式的代码
-s : 生成同步模式的代码
-p <pkg> : 指定代码的package名称
-l <languange> : 使用的语言(Java/C) 默认是java
-t : 为代码生成测试用例
-ss : 生成服务端代码 默认不生成
-sd : 生成服务描述文件 services.xml,仅与-ss一同使用
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 生成服务端和客户端的代码
-pn <port_name> : 当WSDL中有多个port时,指定其中一个port
-sn <serv_name> : 选择WSDL中的一个service
-u : 展开data-binding的类
-r <path> : 为代码生成指定一个repository
-ssi : 为服务端实现代码生成接口类
-S : 为生成的源码指定存储路径
-R : 为生成的resources指定存储路径
–noBuildXML : 输出中不生成build.xml文件
–noWSDL : 在resources目录中不生成WSDL文件
–noMessageReceiver : 不生成MessageReceiver类

(3)调用实例如下;

package com.ming.axis2;import java.rmi.RemoteException;import org.apache.axis2.AxisFault;
import org.junit.Test;import com.ming.mobile.MobileCodeWSStub;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfo;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfoResponse;/***
* @ClassName: MobileClientWsdl2
* @Description: TODO(利用axis2插件wsdl2java生成客户端方式调用,只生成一个类,其中含有很多内部类)
* @date 2017年11月9日 下午2:14:03
**/
public class MobileClientWsdl2 {@Testpublic void testCodeClient() {try {// web服务地址,不是wsdl地址String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";//创建客户端实体类MobileCodeWSStub stub = new MobileCodeWSStub(url);//创建调用方法的实体,设置参数GetMobileCodeInfo getMobileCodeInfo = new GetMobileCodeInfo();getMobileCodeInfo.setMobileCode("15932582632");getMobileCodeInfo.setUserID("");//客户端调用方法,并返回xml信息GetMobileCodeInfoResponse mobileCodeInfo = stub.getMobileCodeInfo(getMobileCodeInfo);System.out.println(mobileCodeInfo.getGetMobileCodeInfoResult());} catch (AxisFault e) {e.printStackTrace();} catch (RemoteException e) {e.printStackTrace();}}
}

Axis2调用WebService服务的3种方式(rpc调用不好用)相关推荐

  1. vue 调用webservice_动态调用WebService接口的几种方式

    一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...

  2. Axis2搭建WebService服务

    使用Axis2搭建WebService服务 文章目录 一.服务端部署 1.1 在web.xml配置文件中添加映射路径: 2. 创建目录及文件 3. 新建服务接口 4. 新建接口实现类 5. 发布服务 ...

  3. 一文讲透推荐系统提供web服务的2种方式

    作者丨gongyouliu 编辑丨zandy 来源 | 大数据与人工智能(ID: ai-big-data) 推荐系统是一种信息过滤技术,通过从用户行为中挖掘用户兴趣偏好,为用户提供个性化的信息,减少用 ...

  4. SoapUI调用webservice接口,http+post方式模仿soapui调用webservice接口

    ` SoapUI调用webservice接口,http+post方式模仿soapui调用webservice接口 项目上调用一个第三方公司提的的webservice短信接口,使用了以下几种接口调用技术 ...

  5. java调用restful接口_Java调用RESTful接口的几种方式

    前端一般通过Ajax来调用,后端调用的方式还是挺多的,比如HttpURLConnection,HttpClient,Spring的RestTemplate 服务端代码如下: 服务端接口请求的URL:h ...

  6. 传递function_Excel VBA解读(132): 调用Function过程的4种方式

    学习Excel技术,关注微信公众号: excelperfect 前面的几篇文章讲解了Function过程的语法以及Function过程的优势和一些细节,也穿插使用了调用Function过程的不同方式. ...

  7. Windows注册服务的两种方式,并设置服务开机自启

    目录 第一种方式: 下载instsrv.exe和srvany.exe 用管理员身份运行"命令提示符"工具 用"注册表编辑器"将服务替换为自己需要的功能 修改完注 ...

  8. Http调用第三方接口的两种方式实例《超详细!!!》***

    Http调用第三方接口的两种方式<超详细!!!>* 最近在公司做一些调用第三方接口的工作,查阅了一部分的资料和向前辈以及朋友请教,完成了第三方接口的调用,其实主要是通过第三方提供的文档,完 ...

  9. Windows注册服务的几种方式

    原文地址:Windows注册服务的几种方式 - BIGTREE 方式一:使用Windows自带的sc命令 1.使用管理员权限打开cmd窗口 2.注册服务命令: sc create 服务名 binpat ...

最新文章

  1. SQL SERVER SQLOS的任务调度--微软亚太区数据库技术支持组 官方博客
  2. java数组去重_再谈JavaScript数组去重
  3. java设置text默认内容_Eclipse自定义内容辅助基于默认Java内容辅助结果
  4. 网站api自己怎么写_网站描述怎么写?对网站优化有什么作用?
  5. Java Web 前端高性能优化(二) 1
  6. 2499元起!vivo首款旗舰级平板正式发布 全系标配8GB运行内存
  7. 为 iOS 6 量身打造 Apps
  8. 【最新版1909 (updated Jan 2020)】Windows10操作系统官方原版镜像
  9. MATLAB自带机器学习算法汇总
  10. BLE芯片DA145XX系列:GPIO特殊配置
  11. 虚拟机内linux网络连接,vmware中redhat5虚拟机无法连接网络
  12. python计算小数点后有几位_小学数学有哪些数学计算技巧?
  13. android获取Bitmap对象,获取图片宽高
  14. 电脑打字习惯让人提笔忘字
  15. 前端代码是怎样智能生成的
  16. java预研项目_缓存java框架技术预研3:JAVA缓存技术介绍
  17. ORACLE修改processes和sessions参数
  18. volumetric obscur ence
  19. 按键脚本c语言,按键精灵脚本(示例代码)
  20. 关于多部门协作完成项目使用过程中出现问题互相推卸责任的问题

热门文章

  1. 达尔豪西大学 计算机科学,达尔豪西大学计算机科学本科专业.pdf
  2. nodeJS web端包使用 bower 管理
  3. postman 访问 报401 认证失败,无法访问系统资源
  4. US5M-ASEMI贴片快恢复二极管US5M
  5. python报考软考哪个比较好_软考高级考哪个好?
  6. java实现HTTP协议:POST协议代码实现
  7. echarts label换行
  8. Spark之Column
  9. 爱立信的5G设备订单继续激增,年内或超越华为成为第一大设备商
  10. html在侧边栏,js+css实现全屏侧边栏