java - 使用 java 从 WSDL 创建 Web 服务客户端

标签 java wsdl

我已经为此搜索了好几天,最后想我应该问一下。一般来说,我对 java 很陌生,对 Web 服务功能也比较陌生,所以我很挣扎。我很惊讶在我在网上找到的所有示例中都没有找到有关此问题的分步指南,但我确信某处有一些东西。我需要做的就是将 WSDL 文档导入 Java(完成),并能够从我的类调用 Web 服务,向其发送一个字符串,并让它返回响应。当我第一次将 WSDL 导入项目时,存在几个错误。主要的似乎是它需要一个与生成的代理类类似的接口(interface)。所以我已经这样做了,但也存在其他错误。您必须在生成的类中创建代码并在其中执行您自己的操作,这是否正常,或者 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。

关于java - 使用 java 从 WSDL 创建 Web 服务客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006897/

相关文章:

java - 时区更改会影响 SSL 握手吗?

Java - 访问ArrayList中对象的原始类型

java - 如何在文档中命名方法?

固定到单个内核时,Java 应用程序/JVM 运行速度极慢

C# 在 Visual Studio 中从自动生成的 Reference.cs 获取 XML SOAP 响应

ssl - 让 webservice 使用 SSL

java - PersonResource 与 spring-hateoas 中的 Person 是一样的吗

python 响应与soapUI 不匹配

node.js - 如何通过node js消费wsdl webservice

java - WSDL:您会编写它还是生成它?