WSDL解析

背景

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

wsdl解析

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

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

代码

 1 /**
 2  * WsdlInfo.java Create on 2013-5-4 下午12:56:14
 3  *
 4  * 类功能说明:  wsdl解析入口
 5  *
 6  * Copyright: Copyright(c) 2013
 7  * Company: COSHAHO
 8  * @Version 1.0
 9  * @Author 何科序
10  */
11 public class WsdlInfo
12 {
13     private String wsdlName;
14
15     private List<InterfaceInfo> interfaces;
16
17     /**
18      * coshaho
19      * @param path  wsdl地址
20      * @throws Exception
21      */
22     public WsdlInfo(String path) throws Exception
23     {
24         WProject project = new WProject();
25         WsdlInterface[] wsdlInterfaces = WsdlImporter.importWsdl( project, path );
26         this.wsdlName = path;
27         if(null != wsdlInterfaces)
28         {
29             List<InterfaceInfo> interfaces = new ArrayList<InterfaceInfo>();
30             for(WsdlInterface wsdlInterface : wsdlInterfaces)
31             {
32                 InterfaceInfo interfaceInfo = new InterfaceInfo(wsdlInterface);
33                 interfaces.add(interfaceInfo);
34             }
35             this.interfaces = interfaces;
36         }
37     }
38
39     public String getWsdlName() {
40         return wsdlName;
41     }
42
43     public void setWsdlName(String wsdlName) {
44         this.wsdlName = wsdlName;
45     }
46
47     public List<InterfaceInfo> getInterfaces() {
48         return interfaces;
49     }
50
51     public void setInterfaces(List<InterfaceInfo> interfaces) {
52         this.interfaces = interfaces;
53     }
54 }
 1 /**
 2  *
 3  * InterfaceInfo.java Create on 2016年7月20日 下午9:03:21
 4  *
 5  * 类功能说明: 接口信息
 6  *
 7  * Copyright: Copyright(c) 2013
 8  * Company: COSHAHO
 9  * @Version 1.0
10  * @Author 何科序
11  */
12 public class InterfaceInfo
13 {
14     private String interfaceName;
15
16     private List<OperationInfo> operations;
17
18     private String[] adrress;
19
20     public InterfaceInfo(WsdlInterface wsdlInterface)
21     {
22         this.interfaceName = wsdlInterface.getName();
23
24         this.adrress = wsdlInterface.getEndpoints();
25
26         int operationNum = wsdlInterface.getOperationCount();
27         List<OperationInfo> operations = new ArrayList<OperationInfo>();
28
29         for(int i = 0; i < operationNum; i++)
30         {
31             WsdlOperation operation = ( WsdlOperation )wsdlInterface.getOperationAt( i );
32             OperationInfo operationInfo = new OperationInfo(operation);
33             operations.add(operationInfo);
34         }
35
36         this.operations = operations;
37     }
38
39     public String getInterfaceName() {
40         return interfaceName;
41     }
42
43     public void setInterfaceName(String interfaceName) {
44         this.interfaceName = interfaceName;
45     }
46
47     public List<OperationInfo> getOperations() {
48         return operations;
49     }
50
51     public void setOperations(List<OperationInfo> operations) {
52         this.operations = operations;
53     }
54
55     public String[] getAdrress() {
56         return adrress;
57     }
58
59     public void setAdrress(String[] adrress) {
60         this.adrress = adrress;
61     }
62 }
 1 /**
 2  *
 3  * OperationInfo.java Create on 2016年7月20日 下午9:03:42
 4  *
 5  * 类功能说明:  方法信息
 6  *
 7  * Copyright: Copyright(c) 2013
 8  * Company: COSHAHO
 9  * @Version 1.0
10  * @Author 何科序
11  */
12 public class OperationInfo
13 {
14     private String operationName;
15
16     private String requestXml;
17
18     private String responseXml;
19
20     public OperationInfo(WsdlOperation operation)
21     {
22         operationName = operation.getName();
23         requestXml = operation.createRequest( true );
24         responseXml = operation.createResponse(true);
25     }
26
27     public String getOperationName() {
28         return operationName;
29     }
30
31     public void setOperationName(String operationName) {
32         this.operationName = operationName;
33     }
34
35     public String getRequestXml() {
36         return requestXml;
37     }
38
39     public void setRequestXml(String requestXml) {
40         this.requestXml = requestXml;
41     }
42
43     public String getResponseXml() {
44         return responseXml;
45     }
46
47     public void setResponseXml(String responseXml) {
48         this.responseXml = responseXml;
49     }
50 }

测试代码

 1 package com.coshaho.integration;
 2
 3 import com.coshaho.integration.wsdl.InterfaceInfo;
 4 import com.coshaho.integration.wsdl.OperationInfo;
 5 import com.coshaho.integration.wsdl.WsdlInfo;
 6
 7 /**
 8  *
 9  * WSDLParseTest.java Create on 2016年7月20日 下午9:24:36
10  *
11  * 类功能说明: WSDL解析测试
12  *
13  * Copyright: Copyright(c) 2013
14  * Company: COSHAHO
15  * @Version 1.0
16  * @Author 何科序
17  */
18 public class WSDLParseTest
19 {
20     public static void main(String[] args) throws Exception
21     {
22         String url = "http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl";
23         WsdlInfo wsdlInfo = new WsdlInfo(url);
24         System.out.println("WSDL URL is " + wsdlInfo.getWsdlName());
25
26         for(InterfaceInfo interfaceInfo : wsdlInfo.getInterfaces())
27         {
28             System.out.println("Interface name is " + interfaceInfo.getInterfaceName());
29             for(String ads : interfaceInfo.getAdrress())
30             {
31                 System.out.println("Interface address is " + ads);
32             }
33             for(OperationInfo operation : interfaceInfo.getOperations())
34             {
35                 System.out.println("operation name is " + operation.getOperationName());
36                 System.out.println("operation request is ");
37                 System.out.println("operation request is " + operation.getRequestXml());
38                 System.out.println("operation response is ");
39                 System.out.println(operation.getResponseXml());
40             }
41         }
42     }
43 }

测试结果

  1 WSDL URL is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl
  2 Interface name is ChinaOpenFundWSSoap12
  3 Interface address is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx
  4 operation name is getFundCodeNameDataSet
  5 operation request is
  6 operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
  7    <soap:Header/>
  8    <soap:Body>
  9       <web:getFundCodeNameDataSet/>
 10    </soap:Body>
 11 </soap:Envelope>
 12 operation response is
 13 <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">
 14    <soap:Header/>
 15    <soap:Body>
 16       <web:getFundCodeNameDataSetResponse>
 17          <!--Optional:-->
 18          <web:getFundCodeNameDataSetResult>
 19             <xs:schema>
 20                <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
 21             </xs:schema>
 22             <!--You may enter ANY elements at this point-->
 23          </web:getFundCodeNameDataSetResult>
 24       </web:getFundCodeNameDataSetResponse>
 25    </soap:Body>
 26 </soap:Envelope>
 27 operation name is getFundCodeNameString
 28 operation request is
 29 operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
 30    <soap:Header/>
 31    <soap:Body>
 32       <web:getFundCodeNameString/>
 33    </soap:Body>
 34 </soap:Envelope>
 35 operation response is
 36 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
 37    <soap:Header/>
 38    <soap:Body>
 39       <web:getFundCodeNameStringResponse>
 40          <!--Optional:-->
 41          <web:getFundCodeNameStringResult>
 42             <!--Zero or more repetitions:-->
 43             <web:string>?</web:string>
 44          </web:getFundCodeNameStringResult>
 45       </web:getFundCodeNameStringResponse>
 46    </soap:Body>
 47 </soap:Envelope>
 48 operation name is getOpenFundDataSet
 49 operation request is
 50 operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
 51    <soap:Header/>
 52    <soap:Body>
 53       <web:getOpenFundDataSet>
 54          <!--Optional:-->
 55          <web:userID>?</web:userID>
 56       </web:getOpenFundDataSet>
 57    </soap:Body>
 58 </soap:Envelope>
 59 operation response is
 60 <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">
 61    <soap:Header/>
 62    <soap:Body>
 63       <web:getOpenFundDataSetResponse>
 64          <!--Optional:-->
 65          <web:getOpenFundDataSetResult>
 66             <xs:schema>
 67                <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
 68             </xs:schema>
 69             <!--You may enter ANY elements at this point-->
 70          </web:getOpenFundDataSetResult>
 71       </web:getOpenFundDataSetResponse>
 72    </soap:Body>
 73 </soap:Envelope>
时间: 2024-12-28 20:54:58

WSDL解析的相关文章

java-工具-Webservice wsdl解析

wsdl解析 首先必然是理解第三方webservice的接口描述,也就是解析wsdl文件.wsdl文件是webservice服务接口描述文档,一个wsdl文件可以包含多个接口,一个接口可以包含多个方法. public class WsdlInfo { private String wsdlName; private List<InterfaceInfo> interfaces; /** * coshaho * @param path wsdl地址 * @throws Exception */

基于wsdl的测试数据自动生成技术

1. 大概思路 ①首先导入wsdl文档,进行分析,生成wsdl树或者什么表现形式,得到要输入数据的类型以及约束,然后针对类型约束进行相应的测试数据的生成(生成原则如下图1). 模型生成步骤: 为输入数据建立模型主要有以下几步: 第一步:以一个复杂数据类型的名字作为初始节点(根节点). 第二步:如果根节点的某个子元素仍然是复杂数据类型,那么生成一个该子复杂类型的节点,另外一条以该子复杂类型名为名字的一条边,用以指向该子复杂类型的节点. 第三步:如果根节点的某个子元素是一个基本类型(一种简单数据类型

gsoap开发webservice

gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多.绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户必须改变程序结构来适应相关的类库.与之相反,gSOAP利用编译器技术提供了一组透明化的SOAP API,并将与开发无关的SOAP实现细节相关的内容对用户隐藏起来. gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据结构

Salesforce SOAP 接口集成服务

Salesforce 使用Apex调用外部数据的接口有两种方式:SOAP 和 REST SOAP:Web服务使用XML格式的文件调用服务器,通常需要WSDL文档来生成代码 REST:HTTP使用REST格式的文件调用服务器,推荐使用 基于WSDL的CallOut适用于SOAP方式,HTTP方式可以使用任何的HTTP Service,SOAP或者REST都可以,推荐使用REST方式. 在使用SOAP方式调用接口时,可以借助WSDL2Apex工具将XML文件生成Apex类 在Setup上搜索 Ape

webservice 之 WSDL的解析

先看一个wsdl, <?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:tns="http://ws.lk.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www

WSDL实例解析

WSDL的主要文档元素 WSDL文档可以分为两部分.顶部分由抽象定义组成,而底部分则由具体描述组成.抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随 机器或语言而变的元素.这就定义了一系列服务,截然不同的应用都可以实现.具体部分,如数据的序列化则归入底部分,因为它包含具体的定义.在上述的文档元 素中,<types>.<message>.<portType>属于抽象定义 层,<binding>.<service>属于具体定义层.

如何用Eclipse API 提供的 org.eclipse.wst.wsdl 去解析读取WSDL文件?

相对来说,Eclipse API的中文资料比较少,但是Eclipse的API提供了非常多的,非常强大的功能. 比如,eclipse的Eclipse API 提供的 org.eclipse.wst.wsdl包,里面提供了很多的类去解析WSDL文件. 总体来说,其提供的API简单易懂,而且其API是和专业术语对应起来的,比如, 一个WSDL文档通常包含7个重要的元素,即types.import.message.portType.operation.binding. service元素. 这些元素嵌套

java 调用wsdl接口同时将返回数据解析成json

package com.haiersoft.ushequmobile.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.util.List; import java.util.Map; imp

解析利用wsdl.exe生成webservice代理类的详解

利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部分D:/Program Files/Microsoft Visual Studio 8/VC>wsdl /language:c# /n:TestDemo /out:d:/Temp/TestService.cs D:/Temp/TestService.wsdl在d:/Temp下就会产生一个TestServ