加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

xfire 远程调用 webservice 程序

发布时间:2020-12-17 02:02:28 所属栏目:安全 来源:网络整理
导读:原文地址: http://hi.baidu.com/shirdrn/blog/item/59223b1255c455c8c3fd7815.html ? 现在,我们已经将Web Service应用程序HelloWorld发布到Tomcat服务器上,当然此时Web服务器已经启动,可以向终端用户提供服务。 在此基础上建立客户端测试程序。其实非常

原文地址:
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;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;

public class HelloWorldServiceClient {

??? private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
??? private HashMap endpoints = new HashMap();
??? private Service service0;

??? public HelloWorldServiceClient() {
??????? create0();
??????? Endpoint HelloWorldServiceHttpPortEP = service0 .addEndpoint(new QName("http://webservice.shirdrn.tk","HelloWorldServiceHttpPort"),new QName("http://webservice.shirdrn.tk","HelloWorldServiceHttpBinding"),"http://localhost:8080/HelloWorld/services/HelloWorldService");
??????? endpoints.put(new QName("http://webservice.shirdrn.tk",HelloWorldServiceHttpPortEP);
??????? Endpoint HelloWorldServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://webservice.shirdrn.tk","HelloWorldServicePortTypeLocalEndpoint"),"HelloWorldServicePortTypeLocalBinding"),"xfire.local://HelloWorldService");
??????? endpoints.put(new QName("http://webservice.shirdrn.tk",HelloWorldServicePortTypeLocalEndpointEP);
??? }

??? public Object getEndpoint(Endpoint endpoint) {
??????? try {
??????????? return proxyFactory.create((endpoint).getBinding(),(endpoint).getUrl());
??????? } catch (MalformedURLException e) {
??????????? throw new XFireRuntimeException("Invalid URL",e);
??????? }
??? }

??? public Object getEndpoint(QName name) {
??????? Endpoint endpoint = ((Endpoint) endpoints.get((name)));
??????? if ((endpoint) == null) {
??????????? throw new IllegalStateException("No such endpoint!");
??????? }
??????? return getEndpoint((endpoint));
??? }

??? public Collection getEndpoints() {
??????? return endpoints.values();
??? }

??? private void create0() {
??????? TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
??????? HashMap props = new HashMap();
??????? props.put("annotations.allow.interface",true);
??????? AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(),tm,new AegisBindingProvider(new JaxbTypeRegistry()));
??????? asf.setBindingCreationEnabled(false);
??????? service0 = asf.create((cn.edu.cust.client.HelloWorldServicePortType.class),props);
??????? {
??????????? AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,"http://schemas.xmlsoap.org/soap/http");
??????? }
??????? {
??????????? AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,"urn:xfire:transport:local");
??????? }
??? }

??? public HelloWorldServicePortType getHelloWorldServiceHttpPort() {
??????? return ((HelloWorldServicePortType)(this).getEndpoint(new QName("http://webservice.shirdrn.tk","HelloWorldServiceHttpPort")));
??? }

??? public HelloWorldServicePortType getHelloWorldServiceHttpPort(String url) {
??????? HelloWorldServicePortType var = getHelloWorldServiceHttpPort();
??????? org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
??????? return var;
??? }

??? public HelloWorldServicePortType getHelloWorldServicePortTypeLocalEndpoint() {
??????? return ((HelloWorldServicePortType)(this).getEndpoint(new QName("http://webservice.shirdrn.tk","HelloWorldServicePortTypeLocalEndpoint")));
??? }

??? public HelloWorldServicePortType getHelloWorldServicePortTypeLocalEndpoint(String url) {
??????? HelloWorldServicePortType var = getHelloWorldServicePortTypeLocalEndpoint();
??????? org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
??????? return var;
??? }

??? public static void main(String[] args) {
???????

??????? HelloWorldServiceClient client = new HelloWorldServiceClient();
???????
?? //create a default service endpoint
??????? HelloWorldServicePortType service = client.getHelloWorldServiceHttpPort();
???????
?? //TODO: Add custom client code here
??????? ?? //
??????? ?? //service.yourServiceOperationHere();

???????
?? System.out.println("test client completed");
??????? ?? System.exit(0);
??? }

}

可以看到,至于如何获取到Web Service的复杂过程已经由MyEclipse和XFire框架自动构建了。

修改一下主函数,如下:

???? public static void main(String[] args) {

??????? HelloWorldServiceClient client = new HelloWorldServiceClient();
???????? HelloWorldServicePortType service = client.getHelloWorldServiceHttpPort();
???????? String toMe = service.showGreetings("SHIRDRN");
??????? System.out.println(toMe);
??????? String serverTime = service.getServerTime();
??????? System.out.println(serverTime);
??? }

运行,结果如下所示:

Hello,SHIRDRN!
2008-9-10 22:06:55

从结果可以看到,作为客户端,我们确实可以调用已经发布成功的Web Service了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读