Java 给定 WSDL 的 Web 服务客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4172118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web service client given WSDL
提问by user506518
I am trying to create a web service client in Java. I am not aware of how to do it. This is a URL for WSDL: https://testservices.gatewayedi.com/PayerList/payerlist.asmx?wsdl
我正在尝试用 Java 创建一个 Web 服务客户端。我不知道该怎么做。这是 WSDL 的 URL:https: //testservices.gatewayedi.com/PayerList/payerlist.asmx?wsdl
This is the WSDL:
这是 WSDL:
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="https://ws.gatewayedi.com/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="https://ws.gatewayedi.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="https://ws.gatewayedi.com/">
<s:element name="Ping">
<s:complexType />
</s:element>
<s:element name="PingResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="PingResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="AuthSOAPHeader" type="tns:AuthSOAPHeader" />
<s:complexType name="AuthSOAPHeader">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="User" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
</s:sequence>
<s:anyAttribute />
</s:complexType>
<s:element name="GetXMLPayerList">
<s:complexType />
</s:element>
<s:element name="GetXMLPayerListResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetXMLPayerListResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getDocLinks">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="pid" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getDocLinksResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getDocLinksResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="PingSoapIn">
<wsdl:part name="parameters" element="tns:Ping" />
</wsdl:message>
<wsdl:message name="PingSoapOut">
<wsdl:part name="parameters" element="tns:PingResponse" />
</wsdl:message>
<wsdl:message name="PingAuthSOAPHeader">
<wsdl:part name="AuthSOAPHeader" element="tns:AuthSOAPHeader" />
</wsdl:message>
<wsdl:message name="GetXMLPayerListSoapIn">
<wsdl:part name="parameters" element="tns:GetXMLPayerList" />
</wsdl:message>
<wsdl:message name="GetXMLPayerListSoapOut">
<wsdl:part name="parameters" element="tns:GetXMLPayerListResponse" />
</wsdl:message>
<wsdl:message name="GetXMLPayerListAuthSOAPHeader">
<wsdl:part name="AuthSOAPHeader" element="tns:AuthSOAPHeader" />
</wsdl:message>
<wsdl:message name="getDocLinksSoapIn">
<wsdl:part name="parameters" element="tns:getDocLinks" />
</wsdl:message>
<wsdl:message name="getDocLinksSoapOut">
<wsdl:part name="parameters" element="tns:getDocLinksResponse" />
</wsdl:message>
<wsdl:message name="getDocLinksAuthSOAPHeader">
<wsdl:part name="AuthSOAPHeader" element="tns:AuthSOAPHeader" />
</wsdl:message>
<wsdl:portType name="PayerListSoap">
<wsdl:operation name="Ping">
<wsdl:input message="tns:PingSoapIn" />
<wsdl:output message="tns:PingSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetXMLPayerList">
<wsdl:input message="tns:GetXMLPayerListSoapIn" />
<wsdl:output message="tns:GetXMLPayerListSoapOut" />
</wsdl:operation>
<wsdl:operation name="getDocLinks">
<wsdl:input message="tns:getDocLinksSoapIn" />
<wsdl:output message="tns:getDocLinksSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PayerListSoap" type="tns:PayerListSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Ping">
<soap:operation soapAction="https://ws.gatewayedi.com/Ping" style="document" />
<wsdl:input>
<soap:body use="literal" />
<soap:header message="tns:PingAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetXMLPayerList">
<soap:operation soapAction="https://ws.gatewayedi.com/GetXMLPayerList" style="document" />
<wsdl:input>
<soap:body use="literal" />
<soap:header message="tns:GetXMLPayerListAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDocLinks">
<soap:operation soapAction="https://ws.gatewayedi.com/getDocLinks" style="document" />
<wsdl:input>
<soap:body use="literal" />
<soap:header message="tns:getDocLinksAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="PayerListSoap12" type="tns:PayerListSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Ping">
<soap12:operation soapAction="https://ws.gatewayedi.com/Ping" style="document" />
<wsdl:input>
<soap12:body use="literal" />
<soap12:header message="tns:PingAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetXMLPayerList">
<soap12:operation soapAction="https://ws.gatewayedi.com/GetXMLPayerList" style="document" />
<wsdl:input>
<soap12:body use="literal" />
<soap12:header message="tns:GetXMLPayerListAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDocLinks">
<soap12:operation soapAction="https://ws.gatewayedi.com/getDocLinks" style="document" />
<wsdl:input>
<soap12:body use="literal" />
<soap12:header message="tns:getDocLinksAuthSOAPHeader" part="AuthSOAPHeader" use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PayerList">
<wsdl:port name="PayerListSoap" binding="tns:PayerListSoap">
<soap:address location="https://testservices.gatewayedi.com/PayerList/payerlist.asmx" />
</wsdl:port>
<wsdl:port name="PayerListSoap12" binding="tns:PayerListSoap12">
<soap12:address location="https://testservices.gatewayedi.com/PayerList/payerlist.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
There are three methods in the service.
服务中共有三种方法。
I am trying to call this method using SOAP.
我正在尝试使用 SOAP 调用此方法。
The XML message format is:
XML 消息格式为:
POST /PayerList/payerlist.asmx HTTP/1.1
Host: testservices.gatewayedi.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: <<insert message length here>>
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Header> <AuthSOAPHeader xmlns="https://ws.gatewayedi.com/"> <User><<insert user id here>>
</User>
<Password><<insert user web password here>> </Password>
</AuthSOAPHeader>
</soap12:Header> <soap12:Body> <Ping xmlns="https://ws.gatewayedi.com/" />
</soap12:Body>
</soap12:Envelope>
Anyone who knows the solution please let me know. Any help is really appreciable.
知道解决方法的朋友请告诉我。任何帮助都是非常可观的。
回答by Jon Skeet
EDIT: As Pascal appears to have recent experience on this, it's probably worth reading his comment:
编辑:由于帕斯卡最近在这方面有经验,因此可能值得阅读他的评论:
Nowadays, I wouldn't use Axis (I actually recommend against using it). Apache CXF or JAX-WS RI (which is included in Java 6 and offers wsimport and wsgen command line commands) are IMO much better stacks and easier to use.
现在,我不会使用 Axis(我实际上建议不要使用它)。Apache CXF 或 JAX-WS RI(包含在 Java 6 中并提供 wsimport 和 wsgen 命令行命令)是 IMO 更好的堆栈并且更易于使用。
So, look at the Apache CXF documentationand the introduction to JAX-WS.
因此,请查看Apache CXF 文档和JAX-WS 介绍。
Original answer
原答案
Have you tried using Apache Axis? It's a while since I've done any Java web services, but that was what I used last time... You'll want to look at WSDL2Javato generate code from the WSDL.
您是否尝试过使用Apache Axis?我已经有一段时间没有做过任何 Java Web 服务了,但那是我上次使用的……您将需要查看WSDL2Java以从 WSDL 生成代码。
回答by Grodriguez
I would recomment to have a look at Apache Axis2 or Apache WSIF (Web Services Invocation Framework).
我建议看看 Apache Axis2 或 Apache WSIF(Web 服务调用框架)。
Links:
链接:
回答by npinti
Apart from what has been suggested above, you can also use an IDE such as Netbeans. It basically allows you to create a webservice client through wizards, without having to go through making all the configurations yourself. Thisvideo shows you how to first create a webservice and then how to create its corresponding client. Note that in the video, whoever is doing the tutorial consumes the webservice through another web application. If you want to consume the webservice through a desktop application, just do the same procedure, the only difference is that instead of a webproject, you do it on the project you are working on.
除了上面建议的内容之外,您还可以使用 IDE,例如Netbeans。它基本上允许您通过向导创建 Web 服务客户端,而无需自己进行所有配置。该视频向您展示了如何首先创建 Web 服务,然后如何创建其相应的客户端。请注意,在视频中,无论是谁在执行教程,都将通过另一个 Web 应用程序使用 Web 服务。如果您想通过桌面应用程序使用 webservice,只需执行相同的过程,唯一的区别是不是 webproject,而是在您正在处理的项目上执行。
回答by Pascal Thivent
As hinted in a comment to Jon's answer, my recommendation would be to use a JAX-WS implementation like JAX-WS RI (which is included in Java 6) or Apache CXF.
正如对Jon 的回答的评论所暗示的那样,我的建议是使用 JAX-WS 实现,如 JAX-WS RI(包含在 Java 6 中)或 Apache CXF。
I'll use JAX-WS RI to illustrate my answer as it's available out of the box, on the command line (to explain the steps) but I'd recommend using an IDE with good JAX-WS support e.g. NetBeans (see the resources at the end of the answer).
我将使用 JAX-WS RI 来说明我的答案,因为它是开箱即用的,在命令行上(解释步骤)但我建议使用具有良好 JAX-WS 支持的 IDE,例如 NetBeans(请参阅资源在答案的最后)。
1. Generate JAX-WS artifacts from the WSDL
1. 从 WSDL 生成 JAX-WS 工件
First run wsimport
to generate JAX-WS artifacts (to put it simply, the classes you'll need to invoke the web service):
首先运行wsimport
以生成 JAX-WS 工件(简单地说,就是调用 Web 服务所需的类):
wsimport -d generated -extension -keep -p com.gatewayedi.ws -XadditionalHeaders https://testservices.gatewayedi.com/PayerList/payerlist.asmx?wsdl
About the options:
关于选项:
-d
is used to specify the target directory for the generated stuff-extension
is used to allow extensions (the WSDL is using a non-standard SOAP 1.2 binding)-keep
is to keep generated.java
sources (this is will ease the development)-p
is used to specify a package for the generated artifacts-XadditionalHeaders
is used to map additional WSDL headers (that are not part of the input or output contract defined in theportType
operation) to method parameters(this will make invoking the service easier).
-d
用于为生成的东西指定目标目录-extension
用于允许扩展(WSDL 使用非标准 SOAP 1.2 绑定)-keep
是保留生成的.java
源(这将简化开发)-p
用于为生成的工件指定一个包-XadditionalHeaders
用于将附加 WSDL 标头(不属于portType
操作中定义的输入或输出协定的一部分)映射到方法参数(这将使调用服务更容易)。
Sorry if some of the vocabulary is cryptic but, well, welcome to SOAP web services :)
抱歉,如果某些词汇含糊不清,但是,欢迎使用 SOAP Web 服务 :)
2. Implement a client
2. 实现一个客户端
Here is a simple client showing how to invoke one of the available operations using the generated classes:
这是一个简单的客户端,展示了如何使用生成的类调用可用操作之一:
import com.gatewayedi.ws.AuthSOAPHeader;
import com.gatewayedi.ws.PayerList;
import com.gatewayedi.ws.PayerListSoap;
public class Main {
public static void main(String[] args) {
new Main().callWebService();
}
private void callWebService() {
PayerList service = new PayerList();
PayerListSoap port = service.getPayerListSoap();
AuthSOAPHeader authSOAPHeader = new AuthSOAPHeader();
authSOAPHeader.setUser("test");
authSOAPHeader.setPassword("test");
String payerList = port.ping(authSOAPHeader);
System.out.println(payerList);
}
}
Below, the generated request:
下面是生成的请求:
<?xml version="1.0" standalone="no"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<AuthSOAPHeader xmlns="https://ws.gatewayedi.com/">
<User>test</User>
<Password>test</Password>
</AuthSOAPHeader>
</S:Header>
<S:Body>
<Ping xmlns="https://ws.gatewayedi.com/"/>
</S:Body>
</S:Envelope>
Don't know what credentials you're supposed to pass though.
不知道你应该通过什么凭据。
Resources
资源
- Developing JAX-WS Web Service Clients(start here)
- Introducing JAX-WS 2.0 With the Java SE 6 Platform, Part 1
- Creating a Simple Web Service and Client with JAX-WS
- Creating a SOAP client with either Apache CXF or GlassFish Metro(Glen Mazza's blog is a great resources)
- 开发 JAX-WS Web 服务客户端(从这里开始)
- 介绍 JAX-WS 2.0 和 Java SE 6 平台,第 1 部分
- 使用 JAX-WS 创建简单的 Web 服务和客户端
- 使用 Apache CXF 或 GlassFish Metro 创建 SOAP 客户端(Glen Mazza 的博客是一个很好的资源)