soap-ws 获取wsdl中所有方法

本文介绍了如何利用soap-ws Java开源框架从wsdl文件中获取binding节点下的所有operation。通常,wsdl的portType和binding节点会包含相同的operation,但SOAPUI默认获取的是binding节点的。文章详细展示了通过Wsdl类和SoapBuilder接口的getOperations()方法实现这一过程,并以获取天气服务的wsdl作为示例进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

soap-ws Java开源框架,github上是这样解释的:
soap-ws ,A lightweight and easy-to-use Java library to handle SOAP on a purely XML level.
github列举了一些使用方法,地址:https://github.com/reficio/soap-ws。

在使用的过程中,需要通过wsdl地址获取到wsdl中定义的所有方法,也就是operation。wsdl中定义Operation的地方有2个:①portType节点;②binding节点。
一般来说,这2个节点的operation是完全一样的。不过通过SOAPUI访问wsdl,获取的是binding节点中的所有operation。如果2个节点的operation都一样获取哪个节点都可以。

1. 获取binding节点的所有operation

看了一下示例代码,
soap-ws是通过以下的方法获取的operation。
SoapOperation operation = builder.operation().soapAction("http://www.webserviceX.NET/ConversionRate").find();
上述方法必须要知道Action的定义,大部分wsld中的Operation都是没有定义action的。所以通过以上方法不行。
查看接口SoapBuilder,定义了获取operation的方法,获取的是binding节点的所有operation。
public interface SoapBuilder {List<SoapOperation> getOperations();}
那通过wsdl怎么用到接口SoapBuilder的getOperations()呢?
参考示例 SoapBuilder builder = *wsdl.binding()*.localPart("CurrencyConvertorSoap").find();
类Wsdl中提供了获取所有binding的方法public List<QName> getBindings()
完整代码如下:

public static List<String> getBindingOperations(String wsdlUrl) {
    List<String> operationList = new ArrayList();
    List<SoapOperation> soapOperationList = new ArrayList();
    Wsdl parser = Wsdl.parse(wsdlUrl);
    List<QName> bindQnames = parser.getBindings();
    for (QName qName : bindQnames) {
        SoapBuilder soapBuilder = parser.binding().localPart(qName.getLocalPart()).find();
        soapOperationList.addAll(soapBuilder.getOperations());
    }
    for (SoapOperation soapOperation : soapOperationList) {
        operationList.add(soapOperation.getOperationName());
    }
    return operationList;
}

以获取天气为例,测试结果:
wsdlUrl = “http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl”;

[getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro]

2. 获取portType节点的所有operation

获取portType节点的operation比较麻烦,soap-ws没有提供直接可以使用的接口。
不过可以参考soap-ws的源码写一个方法。
soap-ws提供了一些example。参考例子:
soap-ws\soap-examples\quickstart\src\test\java\org\reficio\ws\quickstart\SoapClientExamplesTest.java
在IntelliJ IDEA中debug,eclipse中看起来不舒服。

在这里插入图片描述

可以看到在wsdl的私有成员soapFacade中有portType节点,binding节点信息。这些信息最终是定义在messageBuilder的私有变量definition中的,按照这个路径查看源代码。查看SoapMessageBuilder的构造方法。

public SoapMessageBuilder(URL wsdlUrl) throws WSDLException {
    WSDLReader reader = new WSDLReaderImpl();
    reader.setFeature("javax.wsdl.verbose", false);
    this.definition = reader.readWSDL(wsdlUrl.toString());
    this.definitionWrapper = new SchemaDefinitionWrapper(definition, wsdlUrl.toString());
}

根据上面的代码进行改造。完整的代码如下:

 private static List<Operation> getPortTypeOperations(String wsdlUrl) {
        List<Operation> operationList = new ArrayList();
        try {
            WSDLReader reader = new WSDLReaderImpl();
            reader.setFeature("javax.wsdl.verbose", false);
            Definition definition = reader.readWSDL(wsdlUrl.toString());
            Map<String, PortTypeImpl> defMap = definition.getAllPortTypes();
            Collection<PortTypeImpl> collection = defMap.values();
            for (PortTypeImpl portType : collection) {
                operationList.addAll(portType.getOperations());
            }
        } catch (WSDLException e) {
            System.out.println("get wsdl operation fail.");
            e.printStackTrace();
        }
        return operationList;
    }

获取operation的名字

public static List<String> getOperationByUrl(String wsdlUrl) {
    List<String> resultList = new ArrayList<>();
    List<Operation> operationList = getPortTypeOperations(wsdlUrl);
    for (Operation operation : operationList) {
        resultList.add(operation.getName());
    }
    return resultList;
}

上面的方法也可以获取Binding节点的operation。代码如下

public static List<String> getAllBindingOperation(String wsdlUrl) {
    List<BindingOperation> operationList = new ArrayList();
    List<String> nameList = new ArrayList();
    try {
        WSDLReader reader = new WSDLReaderImpl();
        reader.setFeature("javax.wsdl.verbose", false);
        Definition definition = reader.readWSDL(wsdlUrl.toString());
        Map<String, BindingImpl> defMap = definition.getAllBindings();
        Collection<BindingImpl> collection = defMap.values();
        for (BindingImpl binding : collection) {
            operationList.addAll(binding.getBindingOperations());
        }
        for (BindingOperation operation:operationList) {
            nameList.add(operation.getName());
        }
    } catch (WSDLException e) {
        System.out.println("get wsdl operation fail.");
        e.printStackTrace();
    }
    return nameList;
}

3. 两种方法比较

测试方法

    @Test
    public void test2() {
        String wsdlUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        long s1=System.currentTimeMillis();
        System.out.println(getBindingOperations(wsdlUrl));
        long e1=System.currentTimeMillis();
        System.out.println("getBindingOperations "+(e1-s1));//1707ms
        long s2=System.currentTimeMillis();
        System.out.println(getOperationByUrl(wsdlUrl));
        long e2=System.currentTimeMillis();
        System.out.println("getOperationByUrl "+(e2-s2));//199ms
        System.out.println(Arrays.equals(getBindingOperations(wsdlUrl).toArray(),getOperationByUrl(wsdlUrl).toArray()));
    }

测试结果:

[getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro]
getBindingOperations **1123**
[getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro]
getOperationByUrl **35**
false

执行时间上差距很大,原因是第一种方法每次都是新建一个对象。

    static SoapOperationBuilder create(SoapBuilder builder, Binding binding, BindingOperation operation, String soapAction) {
        String bindingInputName = operation.getBindingInput() != null ? operation.getBindingInput().getName() : null;
        String bindingOutputName = operation.getBindingOutput() != null ? operation.getBindingOutput().getName() : null;
        return new SoapOperationImpl(builder, binding.getQName(), operation.getName(), bindingInputName, bindingOutputName,
                SoapUtils.normalizeSoapAction(soapAction));
    }

第二中方法获取到的Binding节点和获取portType节点比较。

    @Test
    public void test03(){
        String wsdlUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        long s1=System.currentTimeMillis();
        System.out.println(getAllBindingOperation(wsdlUrl));
        long e1=System.currentTimeMillis();
        System.out.println("getAllBindingOperation "+(e1-s1));//480ms
        long s2=System.currentTimeMillis();
        System.out.println(getOperationByUrl(wsdlUrl));
        long e2=System.currentTimeMillis();
        System.out.println("getOperationByUrl "+(e2-s2));//281ms
        System.out.println(Arrays.equals(getAllBindingOperation(wsdlUrl).toArray(),getOperationByUrl(wsdlUrl).toArray()));
    }

测试结果

[getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro]
getAllBindingOperation 265
[getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro, getSupportCity, getSupportProvince, getSupportDataSet, getWeatherbyCityName, getWeatherbyCityNamePro]
getOperationByUrl 39
false

综上,获取portType节点的operation时间最快。生产环境推荐获取binding节点的信息。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无心水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值