soap-ws獲取ws中的所有的接口方法


soap-ws獲取wsdl中的所有的接口方法

示例wsdl文件如下,生成的過程可以參考https://www.cnblogs.com/chenyun-/p/11502446.html

 1 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.chenyun.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.chenyun.com/" name="WsImplService">
 2 <types>
 3 <xsd:schema>
 4 <xsd:import namespace="http://impl.chenyun.com/" schemaLocation="http://localhost:8081/Webservice?xsd=1"/>
 5 </xsd:schema>
 6 </types>
 7 <message name="sayname2">
 8 <part name="parameters" element="tns:sayname2"/>
 9 </message>
10 <message name="sayname2Response">
11 <part name="parameters" element="tns:sayname2Response"/>
12 </message>
13 <message name="sayname">
14 <part name="parameters" element="tns:sayname"/>
15 </message>
16 <message name="saynameResponse">
17 <part name="parameters" element="tns:saynameResponse"/>
18 </message>
19 <portType name="WsImpl">
20 <operation name="sayname2">
21 <input wsam:Action="http://impl.chenyun.com/WsImpl/sayname2Request" message="tns:sayname2"/>
22 <output wsam:Action="http://impl.chenyun.com/WsImpl/sayname2Response" message="tns:sayname2Response"/>
23 </operation>
24 <operation name="sayname">
25 <input wsam:Action="http://impl.chenyun.com/WsImpl/saynameRequest" message="tns:sayname"/>
26 <output wsam:Action="http://impl.chenyun.com/WsImpl/saynameResponse" message="tns:saynameResponse"/>
27 </operation>
28 </portType>
29 <binding name="WsImplPortBinding" type="tns:WsImpl">
30 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
31 <operation name="sayname2">
32 <soap:operation soapAction=""/>
33 <input>
34 <soap:body use="literal"/>
35 </input>
36 <output>
37 <soap:body use="literal"/>
38 </output>
39 </operation>
40 <operation name="sayname">
41 <soap:operation soapAction=""/>
42 <input>
43 <soap:body use="literal"/>
44 </input>
45 <output>
46 <soap:body use="literal"/>
47 </output>
48 </operation>
49 </binding>
50 <service name="WsImplService">
51 <port name="WsImplPort" binding="tns:WsImplPortBinding">
52 <soap:address location="http://localhost:8081/Webservice"/>
53 </port>
54 </service>
55 </definitions>

通過wsdl地址獲取的所有方法,也就是operations,在wsdl文件中定義這些operation的地方是有兩處,一處在portType節點,一處是在binding節點處,兩處均可以獲取到所有的方法,具體有什么差別暫時還沒有發現,這里使用的是在binding節點處獲取,在portType處獲取暫時沒有測試,后續可能會更新。

soap-ws的git地址:https://github.com/reficio/soap-ws

在Readme中可以找到Quick-start,根據里面的講解,我們需要使用的部分需要添加一個依賴和一個倉庫(添加倉庫是因為還沒有添加進maven的核心倉庫,文檔中有介紹)

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.8.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.chenyun</groupId>
12     <artifactId>webservice_test_v5</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>webservice_test_v5</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-test</artifactId>
30             <scope>test</scope>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.reficio</groupId>
35             <artifactId>soap-builder</artifactId>
36             <version>1.0.0-SNAPSHOT</version>
37         </dependency>
38     </dependencies>
39 
40     <repositories>
41         <repository>
42             <id>reficio</id>
43             <url>http://repo.reficio.org/maven/</url>
44         </repository>
45     </repositories>
46 
47     <build>
48         <plugins>
49             <plugin>
50                 <groupId>org.springframework.boot</groupId>
51                 <artifactId>spring-boot-maven-plugin</artifactId>
52             </plugin>
53         </plugins>
54     </build>
55 </project>

至此,准備已經大概完成,下面是介紹核心代碼:

getList.java

 1 /*
 2  * @author:陳雲
 3  * @date:2019/9/10
 4  * @description:返回接口列表
 5  */
 6 package com.chenyun;
 7 
 8 import org.reficio.ws.builder.SoapBuilder;
 9 import org.reficio.ws.builder.SoapOperation;
10 import org.reficio.ws.builder.core.Wsdl;
11 
12 import javax.xml.namespace.QName;
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 public class getList {
17     /**
18      *
19      * @param wsdlUrl
20      * @return  List<String>接口列表
21      */
22     public static List<String> getBindingOperations(String wsdlUrl) {
23         List<String> operationList = new ArrayList();
24         List<SoapOperation> soapOperationList = new ArrayList();
25         //解析指定的wsdl文件,生成一個wsdl對象
26         Wsdl parser = Wsdl.parse(wsdlUrl);
27         //類Wsdl中提供了獲取所有binding的方法getBindings()
28         List<QName> bindQnames = parser.getBindings();
29         //接口SoapBuilder,定義了獲取operation的方法,獲取的是binding節點的所有operation。
30         for (QName qName : bindQnames) {
31             SoapBuilder soapBuilder = parser.binding().localPart(qName.getLocalPart()).find();
32             soapOperationList.addAll(soapBuilder.getOperations());
33         }
34         for (SoapOperation soapOperation : soapOperationList) {
35             operationList.add(soapOperation.getOperationName());
36         }
37         return operationList;
38     }
39 }
WebserviceTestV5Application.java
package com.chenyun;

import org.apache.log4j.BasicConfigurator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static com.chenyun.getList.getBindingOperations;

@SpringBootApplication
public class WebserviceTestV5Application {

    public static void main(String[] args) {
        SpringApplication.run(WebserviceTestV5Application.class, args);
        //提供wsdl的值,調用getBindingOperations()
        String wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        System.out.println("=====================================");
        System.out.println("接口列表:");
        for(int i=0;i<getBindingOperations(wsdl).size();i++){
            System.out.println("接口" + i + ":"+ getBindingOperations(wsdl).get(i));
        }
        System.out.println("=====================================");
        System.out.println("接口查詢結束");
    }
}

運行主函數即可獲取到結果。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM