WebService客户端Axis2调用
2016-07-22 15:38
459 查看
1.RPC方式调用
RPCServiceClient方式不能携带指定参数,下列为ServiceClient方式:public static void main(String[] args) { try { String soapBindingAddress = "http://192.168.10.1:8080/BillService.asmx?wsdl"; ServiceClient sender = new ServiceClient(); EndpointReference endpointReference = new EndpointReference(soapBindingAddress); Options options = new Options(); <span style="white-space:pre"> </span> // 命名空间加方法名 options.setAction("http://tempuri.org/PuBillVouchInterface"); options.setTo(endpointReference); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); // 设置命名空间 OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "PuBillVouchInterface"); OMElement data = fac.createOMElement("PuBillVouchInterface", omNs); String dataXml = "<?xml version='1.0' encoding='UTF-8'?>" + "<DataTable>" + "<DataRow>" + "<bill_no>AB002324E1</bill_no>" + "<ddate>2016-7-22</ddate>" + "<cBillType>2</cBillType>" + "<DataDetails>" + "<iquantity>1</iquantity>" + "<iprice>12.2</iprice>" + "<isum>12.2</isum>" + "</DataDetails>" + "<DataDetails>" + "<iquantity>12</iquantity>" + "<iprice>30</iprice>" + "<isum>360</isum>" + "</DataDetails>" + "</DataRow>" + "</DataTable>"; String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase(); // 对应参数的节点 String[] strs = new String[]{"dataXml","certificateHeader"}; // 参数值 String[] val = new String[]{dataXml, md5}; for (int i = 0; i < strs.length; i++) { OMElement inner = fac.createOMElement(strs[i], omNs); inner.setText(val[i]); data.addChild(inner); } // 发送数据,返回结果 OMElement result = sender.sendReceive(data); System.out.println(result.toString()); System.out.println(result.getFirstElement().getText()); } catch (AxisFault ex) { ex.printStackTrace(); } }
2.借助idea生成类文件进行调用
首先下载axis2包文件,地址:http://axis.apache.org/axis2/java/core/download.cgi解压后配置如下:
在项目某目录下,右键-->WebService-->Generate Java Code From Wsdl,按提示配置生成文件即可。生成YBServiceCallbackHandler.java、YBServiceStub两个文件
调用示例如下:
public static void main(String[] args) { try { String dataXml = "<?xml version='1.0' encoding='UTF-8'?>" + "<DataTable>" + "<DataRow>" + "<bill_no>1322k322g2301</bill_no>" + "<ddate>2016-7-8</ddate>"+ "</DataRow>" + "</DataTable>"; String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase(); YBServiceStub stub = new YBServiceStub("http://192.168.10.1:8080/BillService.asmx?wsdl"); YBServiceStub.PuBillVouchInterface request = new YBServiceStub.PuBillVouchInterface(); request.setCertificateHeader(md5); request.setDataXml(dataXml); YBServiceStub.PuBillVouchInterfaceResponse rs = stub.puBillVouchInterface(request); System.out.println(rs.getPuBillVouchInterfaceResult()); } catch (Exception axisFault) { axisFault.printStackTrace(); } }
需要Axis2相关依赖:
<properties> <axis2.version>1.6.2</axis2.version> </properties> <!-- axis2 --> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-http</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-local</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.4.0</version> </dependency> <!-- 1.2.10会有异常 --> <dependency> <groupId>org.apache.ws.commons.axiom</groupId> <artifactId>axiom-api</artifactId> <version>1.2.13</version> </dependency>
相关文章推荐
- C#使用GET、POST请求获取结果
- ("").equals(str)与str.equals("")的区别
- 大数据学习之测试hdfs和mapreduce(二)
- 事件g_Events
- java中InvocationHandler 用于实现代理。
- web缓存技术和HTTP代理
- ExpandableListView方法详解
- SQL的几种语言
- leetcode_c++:栈:Binary Tree Preorder Traversal(144)
- vagrant入门教程--安装vagrant(2)
- Android多点触控技术
- 简单的保存手机号到通讯录并删除
- Spring回调方法DisposableBean接口
- TP框架基础(一)
- AngularJS 服务(Service)
- 博乐招募开始啦
- 对程序员来说,提高薪水最好的建议是什么?
- Pattern Matcher group 简单例子正则表达式
- shell 十三问
- Android常用的热更新技术框架调研