背景

前面我们介绍过利用javassist动态生成webservice,这种方式可以使得我们系统通过页面配置动态发布webservice服务,做到0代码开发发布北向接口。进一步思考,我们如何0代码开发调用第三方webservice服务呢?

wsdl解析

首先必然是理解第三方webservice的接口描述,也就是解析wsdl文件。wsdl文件是webservice服务接口描述文档,一个wsdl文件可以包含多个接口,一个接口可以包含多个方法。

实际上,wsdl解析是十分困难的工作,网上也没有找到有效的解决办法,最终通过阅读SoapUI源码,找到了完美的解析方法。

代码

 /**
* WsdlInfo.java Create on 2013-5-4 下午12:56:14
*
* 类功能说明: wsdl解析入口
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author 何科序
*/
public class WsdlInfo
{
private String wsdlName; private List<InterfaceInfo> interfaces; /**
* coshaho
* @param path wsdl地址
* @throws Exception
*/
public WsdlInfo(String path) throws Exception
{
WsdlProject project = new WsdlProject();
WsdlInterface[] wsdlInterfaces = WsdlImporter.importWsdl( project, path );
this.wsdlName = path;
if(null != wsdlInterfaces)
{
List<InterfaceInfo> interfaces = new ArrayList<InterfaceInfo>();
for(WsdlInterface wsdlInterface : wsdlInterfaces)
{
InterfaceInfo interfaceInfo = new InterfaceInfo(wsdlInterface);
interfaces.add(interfaceInfo);
}
this.interfaces = interfaces;
}
} public String getWsdlName() {
return wsdlName;
} public void setWsdlName(String wsdlName) {
this.wsdlName = wsdlName;
} public List<InterfaceInfo> getInterfaces() {
return interfaces;
} public void setInterfaces(List<InterfaceInfo> interfaces) {
this.interfaces = interfaces;
}
}
 /**
*
* InterfaceInfo.java Create on 2016年7月20日 下午9:03:21
*
* 类功能说明: 接口信息
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author 何科序
*/
public class InterfaceInfo
{
private String interfaceName; private List<OperationInfo> operations; private String[] adrress; public InterfaceInfo(WsdlInterface wsdlInterface)
{
this.interfaceName = wsdlInterface.getName(); this.adrress = wsdlInterface.getEndpoints(); int operationNum = wsdlInterface.getOperationCount();
List<OperationInfo> operations = new ArrayList<OperationInfo>(); for(int i = 0; i < operationNum; i++)
{
WsdlOperation operation = ( WsdlOperation )wsdlInterface.getOperationAt( i );
OperationInfo operationInfo = new OperationInfo(operation);
operations.add(operationInfo);
} this.operations = operations;
} public String getInterfaceName() {
return interfaceName;
} public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
} public List<OperationInfo> getOperations() {
return operations;
} public void setOperations(List<OperationInfo> operations) {
this.operations = operations;
} public String[] getAdrress() {
return adrress;
} public void setAdrress(String[] adrress) {
this.adrress = adrress;
}
}
 /**
*
* OperationInfo.java Create on 2016年7月20日 下午9:03:42
*
* 类功能说明: 方法信息
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author 何科序
*/
public class OperationInfo
{
private String operationName; private String requestXml; private String responseXml; public OperationInfo(WsdlOperation operation)
{
operationName = operation.getName();
requestXml = operation.createRequest( true );
responseXml = operation.createResponse(true);
} public String getOperationName() {
return operationName;
} public void setOperationName(String operationName) {
this.operationName = operationName;
} public String getRequestXml() {
return requestXml;
} public void setRequestXml(String requestXml) {
this.requestXml = requestXml;
} public String getResponseXml() {
return responseXml;
} public void setResponseXml(String responseXml) {
this.responseXml = responseXml;
}
}

测试代码

 package com.coshaho.integration;

 import com.coshaho.integration.wsdl.InterfaceInfo;
import com.coshaho.integration.wsdl.OperationInfo;
import com.coshaho.integration.wsdl.WsdlInfo; /**
*
* WSDLParseTest.java Create on 2016年7月20日 下午9:24:36
*
* 类功能说明: WSDL解析测试
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author 何科序
*/
public class WSDLParseTest
{
public static void main(String[] args) throws Exception
{
String url = "http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl";
WsdlInfo wsdlInfo = new WsdlInfo(url);
System.out.println("WSDL URL is " + wsdlInfo.getWsdlName()); for(InterfaceInfo interfaceInfo : wsdlInfo.getInterfaces())
{
System.out.println("Interface name is " + interfaceInfo.getInterfaceName());
for(String ads : interfaceInfo.getAdrress())
{
System.out.println("Interface address is " + ads);
}
for(OperationInfo operation : interfaceInfo.getOperations())
{
System.out.println("operation name is " + operation.getOperationName());
System.out.println("operation request is ");
System.out.println("operation request is " + operation.getRequestXml());
System.out.println("operation response is ");
System.out.println(operation.getResponseXml());
}
}
}
}

测试结果

 WSDL URL is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl
Interface name is ChinaOpenFundWSSoap12
Interface address is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx
operation name is getFundCodeNameDataSet
operation request is
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
<soap:Header/>
<soap:Body>
<web:getFundCodeNameDataSet/>
</soap:Body>
</soap:Envelope>
operation response is
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Header/>
<soap:Body>
<web:getFundCodeNameDataSetResponse>
<!--Optional:-->
<web:getFundCodeNameDataSetResult>
<xs:schema>
<!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
</xs:schema>
<!--You may enter ANY elements at this point-->
</web:getFundCodeNameDataSetResult>
</web:getFundCodeNameDataSetResponse>
</soap:Body>
</soap:Envelope>
operation name is getFundCodeNameString
operation request is
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
<soap:Header/>
<soap:Body>
<web:getFundCodeNameString/>
</soap:Body>
</soap:Envelope>
operation response is
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
<soap:Header/>
<soap:Body>
<web:getFundCodeNameStringResponse>
<!--Optional:-->
<web:getFundCodeNameStringResult>
<!--Zero or more repetitions:-->
<web:string>?</web:string>
</web:getFundCodeNameStringResult>
</web:getFundCodeNameStringResponse>
</soap:Body>
</soap:Envelope>
operation name is getOpenFundDataSet
operation request is
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
<soap:Header/>
<soap:Body>
<web:getOpenFundDataSet>
<!--Optional:-->
<web:userID>?</web:userID>
</web:getOpenFundDataSet>
</soap:Body>
</soap:Envelope>
operation response is
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Header/>
<soap:Body>
<web:getOpenFundDataSetResponse>
<!--Optional:-->
<web:getOpenFundDataSetResult>
<xs:schema>
<!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
</xs:schema>
<!--You may enter ANY elements at this point-->
</web:getOpenFundDataSetResult>
</web:getOpenFundDataSetResponse>
</soap:Body>
</soap:Envelope>

WSDL解析的更多相关文章

  1. WSDL4J解析WSDL文件方法

    利用wsdl4j解析WSDL文件 工具:wsdl4j1.6 解析wsdl文件是axis1.4的服务wsdl文件 wsdl文件: <?xml version="1.0" enc ...

  2. java环境下wsimport编译Wsdl

    使用命令提示符进行操作:首先CD至java jdk/bin目录下.先bin目录下执行以下命令即可: -----------------------------服务需求放置的位置------------ ...

  3. 通用的调用WebService的两种方法。(调用别人提供的wsdl)(转)

    转载自:http://blog.sina.com.cn/s/blog_65933e020101incz.html1.调用WebService的Client端采用jax-ws调用WebService:流 ...

  4. spring WebServiceTemplate 调用 axis1.4 发布的webservice

     前言: 最近在开发中需要调用对方的 webservice服务,按照现有的技术,本应该是一件很简单的事情,只需要拿到wsdl文件,生成客户端代码即可,但是,对方的webservice服务是06年用ax ...

  5. IOS开发笔记 - 基于wsdl2objc调用webservice

    为了方便在ios下调用webserivce,找来了wsdl2objc这样一个开源的框架来解析webservice方便在ios下引用. 下面做个小例子. 1.首先是用Asp.net搭建一个测试的webs ...

  6. WebService概述

    一.WebService介绍 什么是WebService? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编 ...

  7. 微软.NET Framework cve-2017-8759 复现

    0x00 漏洞前言 FireEye公司最近发现一份恶意微软Office RTF文档,其中利用到一项SOAP WSDL解析器代码注入漏洞——编号CVE-2017-8759.该漏洞允许恶意人士在解析SOA ...

  8. gsoap 学习 1-由wsdl文件生成h头文件

    开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...

  9. gsoap开发webservice

    gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多.绝大多数的C++web服务工具包提供一组API函数类库来处 ...

随机推荐

  1. 【POJ2888】Magic Bracelet Burnside引理+欧拉函数+矩阵乘法

    [POJ2888]Magic Bracelet 题意:一个长度为n的项链,有m种颜色的珠子,有k个限制(a,b)表示颜色为a的珠子和颜色为b的珠子不能相邻,求用m种珠子能串成的项链有多少种.如果一个项 ...

  2. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  3. Mysql带返回值与不带返回值的2种存储过程

    过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...

  4. thinkCMF----列表页跳转

    thinkCMF列表循环有个:用来循环文章列表. <php> $where=[ 'post.create_time'=>['egt',0] ]; $page=[ 'list_rows ...

  5. Django---Mysql数据库链接

    Django链接Mysql数据库: 第一步:创建应用 python manage.py startapp index 第二步:将应用添加到配置里面 settings INSTALLED_APPS = ...

  6. 分布式存储中HDFS与Ceph两者的区别是什么,各有什么优势?

    过去两年,我的主要工作都在Hadoop这个技术栈中,而最近有幸接触到了Ceph.我觉得这是一件很幸运的事,让我有机会体验另一种大型分布式存储解决方案,可以对比出HDFS与Ceph这两种几乎完全不同的存 ...

  7. Django的视图与网址之加法计算

    在最新的Django2.1中,views.py中采用的地址映射方式发生了变化,通过一个加法运算我们来看一看. 方法一:在视图views.py中定义视图逻辑,求解两个数的加法运算:c = a + b,定 ...

  8. Oracle故障排查之oracle解决锁表问题

    --step 1:查看被阻塞会话等待事件 select sid, event, username, lockwait, sql.sql_text  from v$session s, v$sql sq ...

  9. IRC and security tools

    login:::   /msg NickServ identify <password>. join:::   /join #metasploit 浏览器: Tor操作系统: Tails加 ...

  10. linker command failed with exit code 1 (use -v to see invocation) 变量重名

    有时候,xcode报错看不到,点最后一个按钮,类似气泡的就能看到 报错信息: duplicate symbol _imgNummmm in:    /Users/mianmian/Library/De ...