繁体   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