原文地址:
http://hi.baidu.com/shirdrn/blog/item/59223b1255c455c8c3fd7815.html
现在,我们已经将Web Service应用程序HelloWorld发布到Tomcat服务器上,当然此时Web服务器已经启动,可以向终端用户提供服务。
在此基础上建立客户端测试程序。其实非常简单了,使用MyEclipse 6和XFire框架会自动生成我们的客户端测试程序,方便调用已发布的Web Service。
由于发布Web Service成功之后,服务的定义和描述生成了,即WSDL文件显示的内容,它是基于XSD的,具有跨平台的特性,因此可以使用基于任何编程语言的客户端应用来进行测试。我选择使用Java Project。
建立的客户端测试工程为HelloWorldClient,新建包cn.edu.cust.client,然后创建一个Web Service Client文件,如图所示:
接下来的设置就比较重要了,因为在客户端你需要找到已经发布的Web Service,这样才能够使用发布的服务。如图所示:
对于Service DeFinition选项,可以直接选择WSDL File,也可以选择使用WSDL URL。这里我使用后者。发布的WSDL URL为:
http://localhost:8080/HelloWorld/services/HelloWorldService?WSDL
单击“Next”,如果失败,则会提示错误信息;如果成功,如图所示:
单击“Next”,客户端建立完成,然后可以编写测试代码。这时,客户端测试工程HelloWorldClient中增加了一个包及其文件,如图所示:
现在,我们再看一看发布的WSDL文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:deFinitions targetNamespace="http://webservice.shirdrn.tk" xmlns:tns="http://webservice.shirdrn.tk" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://webservice.shirdrn.tk"> <xsd:element name="getServerTime"> <xsd:complexType/> </xsd:element> <xsd:element name="getServerTimeResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="showGreetings"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="showGreetingsResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="showGreetingsResponse"> <wsdl:part name="parameters" element="tns:showGreetingsResponse"> </wsdl:part> </wsdl:message> <wsdl:message name="showGreetingsRequest"> <wsdl:part name="parameters" element="tns:showGreetings"> </wsdl:part> </wsdl:message> <wsdl:message name="getServerTimeResponse"> <wsdl:part name="parameters" element="tns:getServerTimeResponse"> </wsdl:part> </wsdl:message> <wsdl:message name="getServerTimeRequest"> <wsdl:part name="parameters" element="tns:getServerTime"> </wsdl:part> </wsdl:message> <wsdl:portType name="HelloWorldServicePortType"> <wsdl:operation name="getServerTime"> <wsdl:input name="getServerTimeRequest" message="tns:getServerTimeRequest"> </wsdl:input> <wsdl:output name="getServerTimeResponse" message="tns:getServerTimeResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="showGreetings"> <wsdl:input name="showGreetingsRequest" message="tns:showGreetingsRequest"> </wsdl:input> <wsdl:output name="showGreetingsResponse" message="tns:showGreetingsResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldServiceHttpBinding" type="tns:HelloWorldServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getServerTime"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="getServerTimeRequest"> <wsdlsoap:body use="literal"/> </wsdl:input> <wsdl:output name="getServerTimeResponse"> <wsdlsoap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="showGreetings"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="showGreetingsRequest"> <wsdlsoap:body use="literal"/> </wsdl:input> <wsdl:output name="showGreetingsResponse"> <wsdlsoap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldService"> <wsdl:port name="HelloWorldServiceHttpPort" binding="tns:HelloWorldServiceHttpBinding"> <wsdlsoap:address location="http://localhost:8080/HelloWorld/services/HelloWorldService"/> </wsdl:port> </wsdl:service> </wsdl:deFinitions> |
上面wsdl:types元素中定义了类型,在客户端应用中映射为对应的类型类,也就是xsd:element元素中的name属性对应的属性值映射为tk.shirdrn.webservice包中的类。
要想测试是否能够调用已经发布的Web Service,只需要在cn.edu.cust.client.HelloWorldServiceClient类中的主函数中编写调用Web Service的测试代码。
看一下自动生成的cn.edu.cust.client.HelloWorldServiceClient类,如下所示:
package cn.edu.cust.client; import java.net.MalformedURLException; public class HelloWorldServiceClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory(); public HelloWorldServiceClient() { public Object getEndpoint(Endpoint endpoint) { public Object getEndpoint(QName name) { public Collection getEndpoints() { private void create0() { public HelloWorldServicePortType getHelloWorldServiceHttpPort() { public HelloWorldServicePortType getHelloWorldServiceHttpPort(String url) { public HelloWorldServicePortType getHelloWorldServicePortTypeLocalEndpoint() { public HelloWorldServicePortType getHelloWorldServicePortTypeLocalEndpoint(String url) { public static void main(String[] args) { HelloWorldServiceClient client = new HelloWorldServiceClient(); } |
可以看到,至于如何获取到Web Service的复杂过程已经由MyEclipse和XFire框架自动构建了。
public static void main(String[] args) { HelloWorldServiceClient client = new HelloWorldServiceClient(); |
运行,结果如下所示:
Hello,SHIRDRN! |
从结果可以看到,作为客户端,我们确实可以调用已经发布成功的Web Service了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。