簡體   English   中英

在Java中從WSDL創建Web服務客戶端

[英]Creating a web service client from WSDL in java

我一直在尋找這個問題的日子,終於想通了。 我是Java的新手,而Web服務的功能是新手,所以我很努力。 令我驚訝的是,我在網上找到的所有示例中都沒有找到有關此操作的分步指南,但是我敢肯定,某些​​地方還是存在一些指導。 我需要做的就是將WSDL文檔導入Java(完成),並能夠從我的類中調用Web服務,將其發送一個字符串,並使其返回響應。 當我第一次將WSDL導入項目時,存在幾個錯誤。 主要的原因似乎是它需要一個與生成的代理類相似的名稱的接口。 所以我已經做到了,但是其他錯誤也在那里。 您必須在生成的類中創建代碼並在其中做您自己的事情是正常的,還是應該將WSDL格式化和精確化,以便即插即用? 我從幾年前的一個項目中想到,它幾乎是即插即用的,您可以將其導入並執行String response = myService.call(String myString); 並僅通過引用WSDL生成的類就使它返回響應。 有沒有人對初學者可以學習如何基於WSDL文件調用Web服務有任何想法? 我的意思是從第一個方框開始,例如您需要導入的東西,需要從中獲取.jar文件引用(如果需要)等等。僅供參考,我在下面包括WSDL。 感謝您的任何見解!

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://listeners.webtools.integrator.myserver.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://listeners.webtools.integrator.myserver.com">
<wsdl:documentation>MYListener</wsdl:documentation>
<wsdl:types>
  <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://listeners.webtools.integrator.myserver.com">
    <xs:element name="processMessage">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="0" name="inputMessage" nillable="true" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
   </xs:element>
                        <xs:element minOccurs="0" name="return"
                         nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
</wsdl:types>

<wsdl:message name="processMessageRequest">
    <wsdl:part name="parameters" element="ns:processMessage"/>
</wsdl:message>
<wsdl:message name="processMessageResponse">
    <wsdl:part name="parameters" element="ns:processMessageResponse"/>
</wsdl:message>
<wsdl:portType name="MYListenerPortType">
    <wsdl:operation name="processMessage">
        <wsdl:input message="ns:processMessageRequest" 
         wsaw:Action="urn:processMessage"/>
        <wsdl:output message="ns:processMessageResponse" 
         wsaw:Action="urn:processMessageResponse"/>
    </wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MYListenerSoap11Binding" 
 type="ns:MYListenerPortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" 
     style="document"/>
    <wsdl:operation name="processMessage">
        <soap:operation soapAction="urn:processMessage" 
         style="document"/>
        <wsdl:input>
            <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MYListenerSoap12Binding" 
 type="ns:MYListenerPortType">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" 
     style="document"/>
    <wsdl:operation name="processMessage">
        <soap12:operation soapAction="urn:processMessage" 
         style="document"/>
        <wsdl:input>
            <soap12:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap12:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>

<wsdl:binding name="MYListenerHttpBinding" type="ns:MYListenerPortType">
    <http:binding verb="POST"/>
    <wsdl:operation name="processMessage">
        <http:operation location="processMessage"/>
        <wsdl:input>
            <mime:content type="application/xml" part="parameters"/>
        </wsdl:input>
        <wsdl:output>
            <mime:content type="application/xml" part="parameters"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:service name="MYListener">
    <wsdl:port name="MYListenerHttpSoap11Endpoint" 
     binding="ns:MYListenerSoap11Binding">
        <soap:address location="http://10.221.55.206:20043/axis2/services/MYListener.MYListenerHttpSoap11Endpoint/"/>
    </wsdl:port>
    <wsdl:port name="MYListenerHttpSoap12Endpoint" binding="ns:MYListenerSoap12Binding">
        <soap12:address location="http://10.221.55.206:20043/axis2/services/MYListener.MYListenerHttpSoap12Endpoint/"/>
    </wsdl:port>
    <wsdl:port name="MYListenerHttpEndpoint" binding="ns:MYListenerHttpBinding">
        <http:address location="http://10.221.55.206:20043/axis2/services/MYListener.MYListenerHttpEndpoint/"/>
    </wsdl:port>
</wsdl:service>

您可以使用wsimport工具生成所有工件,然后手動進行組織。 或更簡單的方法是,安裝NetBeans並使用其選項“從wsdl生成Web服務客戶端”。 它將所有文件放在正確的位置並生成jax-ws-catalog.xml文件。 更多信息在這里:

https://netbeans.org/kb/docs/websvc/client.html#creatingtheclient

HTH。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM