如何使用Java从WSDL获取SOAPAction

时间:2011-11-05 11:14:50

标签: java soap

我正在使用javax.wsdl包来解析wsdl文件。

我不确定如何从wsdl文件中获取操作的SOAPAction。

我可以使用WSDLFactory获取javax.wsdl.Operation对象。但我发现无法获得该操作的SOAPAction。

有人知道如何获得它吗?

谢谢, Maviswa

1 个答案:

答案 0 :(得分:9)

您需要获取与ExtensibilityElement对应的SOAPOperation并从那里提取SOAPAction

让我们从TempConvert Web Service中以一个简单的WSDL为例,从CelsiusToFahrenheit操作中提取SOAP动作;我正在追寻这一部分:

<wsdl:binding name="TempConvertSoap" type="tns:TempConvertSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="FahrenheitToCelsius">
      <soap:operation soapAction="http://tempuri.org/FahrenheitToCelsius" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="CelsiusToFahrenheit">
      <soap:operation soapAction="http://tempuri.org/CelsiusToFahrenheit" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

以下代码打印CelsiusToFahrenheit操作的SOAP操作的值:

WSDLFactory factory = WSDLFactory.newInstance();
WSDLReader reader = factory.newWSDLReader();
Definition definition = reader.readWSDL("http://www.w3schools.com/webservices/tempconvert.asmx?wsdl");
Binding binding = definition.getBinding(new QName("http://tempuri.org/", "TempConvertSoap"));
BindingOperation operation = binding.getBindingOperation("CelsiusToFahrenheit", null, null); 
List extensions = operation.getExtensibilityElements();
if (extensions != null) {
    for (int i = 0; i < extensions.size(); i++) {
        ExtensibilityElement extElement = (ExtensibilityElement) extensions.get(i); 

        // ....

        if (extElement instanceof SOAPOperation) {
            SOAPOperation soapOp = (SOAPOperation) extElement;
            System.out.println(soapOp.getSoapActionURI());
        }

        // ....
    }
}

输出是这样的:

http://tempuri.org/CelsiusToFahrenheit