码迷,mamicode.com
首页 > 其他好文 > 详细

WSDL解析

时间:2016-07-20 22:56:22      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:

背景

前面我们介绍过利用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>

 

WSDL解析

标签:

原文地址:http://www.cnblogs.com/coshaho/p/5689738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!