在java中从WSDL创建Web服务客户端

时间:2017-09-01 19:34:00

标签: java wsdl

我一直在寻找这个日子,终于想到我会问。我一般都是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>

1 个答案:

答案 0 :(得分:0)

您可以使用wsimport工具生成所有工件,然后手动组织它们。或者更简单,安装NetBeans并使用其选项“从wsdl生成Web服务客户端”。它会将所有文件放在正确的位置并生成jax-ws-catalog.xml文件。更多信息:

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

HTH。