• 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("接口查询结束");
        }
    }

    运行主函数即可获取到结果。

  • 相关阅读:
    JVM垃圾回收
    JVM 新生代与老年代
    java 异常处理
    二叉搜索树转有序双向链表
    java 对象序列化
    java 字符集 Charset
    MySQL 过滤数据(WHERE子句)
    无重复字符的最长子串
    二叉查找树
    MySQL 检索数据(SELECT)
  • 原文地址:https://www.cnblogs.com/chenyun-/p/11502556.html
Copyright © 2020-2023  润新知