`
java_4_ever
  • 浏览: 5154 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

XFire构建web services客户端的五种方式

阅读更多

本文主要源于先前看到的一篇文章,为了自己以后查看方便也就转载过来了

原文链接:http://blog.csdn.net/jadyer/article/details/6082948

另外还有第四种,第五种方法,是从别处看到的 ,在本文最后和大家分享一下

这里并未涉及到JSR 181 Annotations 的相关应用,具体的三种方式如下
 通过WSDL地址来创建动态客户端 
 通过服务端提供的接口来创建客户端 
 使用Ant通过WSDL文件来生成客户端


第一种方式:通过WSDL地址来创建动态客户端

  1. package com.jadyer.client;  
  2. import java.net.MalformedURLException;  
  3. import java.net.URL;  
  4. import org.codehaus.xfire.client.Client;  
  5. /** 
  6.  * 通过WSDL来创建动态客户端 
  7.  * @see 此时需要在项目中引入XFire 1.2 Core Libraries和XFire 1.2 HTTP Client Libraries 
  8.  */  
  9. public class ClientFromWSDL {  
  10.     public static void main(String[] args) throws MalformedURLException, Exception {  
  11.         Client client = new Client(new URL("http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl"));  
  12.         Object[] results11 = client.invoke("sayHello"new Object[]{"Jadyer22"});  
  13.         System.out.println(results11[0]);  
  14.     }  
  15. }  

 

第二种方式:通过服务端提供的端口来创建客户端

  1. package com.jadyer.client;  
  2. import java.net.MalformedURLException;  
  3. import java.util.List;  
  4. import org.codehaus.xfire.client.XFireProxyFactory;  
  5. import org.codehaus.xfire.service.Service;  
  6. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  7. import com.jadyer.model.Person;  
  8. import com.jadyer.model.User;  
  9. import com.jadyer.server.HelloService;  
  10. /** 
  11.  * 通过Web服务端提供的接口来创建客户端 
  12.  * @see 客户端必须提供一个与服务端完全一致的接口,包名也要一致 
  13.  * @see 在本例中,需要在客户端(即该项目)中提供HelloService.java接口,以及Person和User两个POJO类 
  14.  * @see 并且此时需要在项目中引入XFire 1.2 Core Libraries和XFire 1.2 HTTP Client Libraries 
  15.  */  
  16. public class ClientFromInterface {  
  17.     public static void main(String[] args)throws MalformedURLException{  
  18.         //首先使用XFire的ObjectServiceFactory从HelloService接口创建一个服务模型serviceModel  
  19.         //serviceModel包含服务的说明,换句话说,就是服务的元数据  
  20.         //Create a metadata of the service  
  21.         Service serviceModel = new ObjectServiceFactory().create(HelloService.class);  
  22.           
  23.         //访问的地址  
  24.         String serviceURL = "http://127.0.0.1:8080/XFire_demo/services/XFireServer";  
  25.           
  26.         //通过查看org.codehaus.xfire.client.XFireProxyFactory源码发现  
  27.         //下面两行代码与这里直接new XFireProxyFactory()的作用是等效的  
  28.         //XFire xfire = XFireFactory.newInstance().getXFire();  
  29.         //XFireProxyFactory factory = new XFireProxyFactory(xfire);  
  30.         //为XFire获得一个代理工厂对象  
  31.         //Create a proxy for the deployed service  
  32.         XFireProxyFactory factory = new XFireProxyFactory();  
  33.           
  34.         //通过proxyFactory,使用服务模型serviceModel和服务端点URL(用来获得WSDL)  
  35.         //得到一个服务的本地代理,这个代理就是实际的客户端  
  36.         HelloService client = (HelloService)factory.create(serviceModel, serviceURL);  
  37.           
  38.         /** 
  39.          * Invoke the service 
  40.          * @see 调用服务的本地代理(即实际的客户端)中的方法,便得到我们需要的WebServcie 
  41.          */  
  42.           
  43.         /*--处理简单对象--*/  
  44.         String serviceResponse = client.sayHello("Jadyer11");  
  45.         System.out.println(serviceResponse);  
  46.           
  47.         /*--处理对象--*/  
  48.         User u = new User();  
  49.         u.setName("Jadyer99");  
  50.         Person pp = client.getPerson(u);  
  51.         System.out.println(pp.getName());  
  52.           
  53.         /*--处理List--*/  
  54.         List<Person> personList = client.getPersonList(24"Jadyer88");  
  55.         for(Person p : personList){  
  56.             System.out.println(p.getName());  
  57.         }  
  58.     }  
  59. }  

 

这是它要用到的接口和两个POJO类

  1. /** 
  2.  * Web服务提供给客户端的接口 
  3.  * @see 这是第二种方式创建的客户端,要用到的接口 
  4.  */  
  5. package com.jadyer.server;  
  6. import java.util.List;  
  7. import com.jadyer.model.Person;  
  8. import com.jadyer.model.User;  
  9. public interface HelloService {  
  10.     public String sayHello(String name);  
  11.     public Person getPerson(User u);  
  12.     public List<Person> getPersonList(Integer age, String name);  
  13. }  
  14. /** 
  15.  * 第二种方式创建的客户端,要用到的两个POJO类 
  16.  */  
  17. package com.jadyer.model;  
  18. public class User {  
  19.     private String name;  
  20.     /*--getter和setter略--*/  
  21. }  
  22. package com.jadyer.model;  
  23. public class Person {  
  24.     private Integer age;  
  25.     private String name;  
  26.     /*--getter和setter略--*/  
  27. }  

 

第三种方式:使用Ant通过WSDL文件来生成客户端

  1. package com.jadyer.client;  
  2. /** 
  3.  * 使用Ant通过WSDL生成客户端 
  4.  * @see 这里的ClientFromAnt.java是我自己创建的,并非Ant生成 
  5.  * @see 这里要用到的JAR有:xfire-all-1.2.6.jar以及//xfire-distribution-1.2.6//lib//目录中的所有JAR包 
  6.  * @see 我们需要把这些JAR包都拷贝到Web Project//WebRoot//WEB-INF//lib//目录中 
  7.  * @see 然后把build.xml和MyFirstXFireServer.wsdl都拷贝到下Web Project的根目录下即可 
  8.  * @see 关于MyFirstXFireServer.wsdl文件,是我在WebServices服务启动后 
  9.  * @see 访问http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl然后将其另存得到的 
  10.  */  
  11. public class ClientFromAnt {  
  12.     public static void main(String[] args) {  
  13.         XFireServerClient client = new XFireServerClient();  
  14.         //String url = "http://127.0.0.1:8080/XFire_demo/services/XFireServer";  
  15.         //String result = client.getXFireServerHttpPort(url).sayHello("Jadyer33");  
  16.         //上面的两行代码,与下面的这一行代码,同效~~  
  17.         String result = client.getXFireServerHttpPort().sayHello("Jadyer33");  
  18.           
  19.         System.out.println(result);  
  20.     }  
  21. }  

 

用到的Ant文件,如下

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="wsgen" default="wsgen" basedir=".">  
  3.     <path id="classpathId">  
  4.         <fileset dir="./WebRoot/WEB-INF/lib">  
  5.             <include name="*.jar" />  
  6.         </fileset>  
  7.     </path>  
  8.       
  9.     <taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"/>  
  10.       
  11.     <target name="wsgen" description="generate client">  
  12.         <wsgen outputDirectory="./src/" wsdl="MyFirstXFireServer.wsdl" binding="xmlbeans" package="com.jadyer.client" overwrite="true"/>  
  13.     </target>  
  14. </project>  

 

也可以使用下面的这个Ant文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="xfireAnt" basedir="." default="createClientCode">  
  3.     <property name="xfirelib" value="${basedir}/WebRoot/WEB-INF/lib"/>  
  4.     <property name="sources" value="${basedir}/src"/>  
  5.     <path id="classpath">  
  6.         <fileset dir="${xfirelib}">  
  7.             <include name="*.jar"/>  
  8.         </fileset>  
  9.     </path>  
  10.           
  11.     <target name="createClientCode">  
  12.         <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath"/>  
  13.         <wsgen outputDirectory="${sources}" wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl" package="com.jadyer.client" overwrite="true"/>  
  14.     </target>  
  15. </project>  

 

最后我再把MyFirstXFireServer.wsdl的内容,附加上

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <wsdl:definitions targetNamespace="http://www.jadyer.com/XFireDemo"  
  3.     xmlns:tns="http://www.jadyer.com/XFireDemo"  
  4.     xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"  
  5.     xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"  
  6.     xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  7.     xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"  
  8.     xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"  
  9.     xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"  
  10.     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">  
  11.     <wsdl:types>  
  12.         <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  13.                     attributeFormDefault="qualified"  
  14.                     elementFormDefault="qualified"  
  15.                     targetNamespace="http://www.jadyer.com/XFireDemo">  
  16.             <xsd:element name="sayHello">  
  17.                 <xsd:complexType>  
  18.                     <xsd:sequence>  
  19.                         <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />  
  20.                     </xsd:sequence>  
  21.                 </xsd:complexType>  
  22.             </xsd:element>  
  23.             <xsd:element name="sayHelloResponse">  
  24.                 <xsd:complexType>  
  25.                     <xsd:sequence>  
  26.                         <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />  
  27.                     </xsd:sequence>  
  28.                 </xsd:complexType>  
  29.             </xsd:element>  
  30.         </xsd:schema>  
  31.     </wsdl:types>  
  32.     <wsdl:message name="sayHelloRequest">  
  33.         <wsdl:part name="parameters" element="tns:sayHello"></wsdl:part>  
  34.     </wsdl:message>  
  35.     <wsdl:message name="sayHelloResponse">  
  36.         <wsdl:part name="parameters" element="tns:sayHelloResponse"></wsdl:part>  
  37.     </wsdl:message>  
  38.     <wsdl:portType name="XFireServerPortType">  
  39.         <wsdl:operation name="sayHello">  
  40.             <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest">  
  41.             </wsdl:input>  
  42.             <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse">  
  43.             </wsdl:output>  
  44.         </wsdl:operation>  
  45.     </wsdl:portType>  
  46.     <wsdl:binding name="XFireServerHttpBinding" type="tns:XFireServerPortType">  
  47.         <wsdlsoap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" />  
  48.         <wsdl:operation name="sayHello">  
  49.             <wsdlsoap:operation soapAction="" />  
  50.             <wsdl:input name="sayHelloRequest">  
  51.                 <wsdlsoap:body use="literal" />  
  52.             </wsdl:input>  
  53.             <wsdl:output name="sayHelloResponse">  
  54.                 <wsdlsoap:body use="literal" />  
  55.             </wsdl:output>  
  56.         </wsdl:operation>  
  57.     </wsdl:binding>  
  58.     <wsdl:service name="XFireServer">  
  59.         <wsdl:port name="XFireServerHttpPort" binding="tns:XFireServerHttpBinding">  
  60.             <wsdlsoap:address location="http://127.0.0.1:8080/XFire_demo/services/XFireServer" />  
  61.         </wsdl:port>  
  62.     </wsdl:service>  
  63. </wsdl:definitions>  

第四种方法

这种方法用到了spring的jar包,是前几天在找XFire+Spring的资料的时候看到的,在这里也是做个记录。同样的,这种方法和上面所提到的第二种方法在客户端都需要与服务器一样的接口,包名也必须一样。

(1)在src目录下新建client.xml(名字并非特定)

 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>  
    <bean id="baseService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" lazy-init="false" abstract="true"/>
<!--    id的名字作为标识,用于客户端程序中获取service,若有多个service咋在下面添加多个bean即可-->
    <bean id="MathService" parent="baseService">
    	<property name="serviceClass">
    		<value>service.MathService</value>
    	</property>
    	<property name="wsdlDocumentUrl">
    		<value>http://localhost:8080/myservice/mathWebService?wsdl</value>
    	</property>
    </bean>
</beans> 

(2)在程序中调用服务代码非常简单

 

 

ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");

MathService mathService = (MathService)ctx.getBean("MathService");
int result = mathService.add(int one,int two);

 

第五种办法

 

先获取到wsdl文件,命名为mathWebService.wsdl放在客户端的src目录下,接着通过程序访问该wsdl文件,并调用需要的方法。

 

 String wsdl  =   "mathWebService.wsdl " ;  // 对应的WSDL文件 
        Resource resource  =   new  ClassPathResource(wsdl); 
        Client client  =   new  Client(resource.getInputStream(),  null );  // 根据WSDL创建客户实例 
        
        Object[] objArray  =   new  Object[ 2 ];
        objArray[ 0 ]  =   2 ;
        obiArray[1] = 3;
         // 调用特定的Web Service方法 
        Object[] results  =  client.invoke( " add " , objArray);
        System.out.println( " result:  "   +  results[ 0 ]);

 

对于这几种方法,第一种方法如果传递的参数为服务器端的实体对象,这点好像比较麻烦,不知道在客户端建立和服务器端相同的实体类行不行,没有实践,返回结果如果是复杂数据类型的话不知道有没有什么问题,或者如何转换,没有深入研究。而且我个人觉得方法调用不是那么直观。第三种方法里面

  1. wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl" 

 

这个地方我不知道如何添加多个链接,有知道的朋友希望能指明一下,谢谢了。如果只能有这一个服务,那就太囧了。其他的方法倒是不觉得有什么大问题,性能什么的差异并未关注,欢迎大家讨论。

 

分享到:
评论

相关推荐

    MyEclipse+XFire开发Web Services

    而XFire则是一个轻量级的开源框架,用于构建和调用Web Services。结合这两款工具可以高效地完成Web Services的开发工作。 本次实验旨在通过使用MyEclipse集成开发环境以及XFire插件来开发一个简单的Web Services...

    java利用 xfire实现 webservices 服务端与客户端

    Java使用XFire实现Web服务(WebServices)是Java开发中的一种常见技术,它允许应用程序通过网络交换数据。XFire是一个轻量级、高性能的框架,它简化了在Java应用程序中创建和消费Web服务的过程。本篇文章将深入探讨...

    xfire根据WSDL生成客户端代码

    在IT行业中,Web服务是一种广泛使用的接口通信方式,它允许不同的应用程序之间进行数据交换。WSDL(Web Services Description Language)是用于定义Web服务接口的一种XML格式,它详细描述了服务的位置、服务的操作...

    XFire开发Web Services

    XFire是Java中一个轻量级的Web服务实现,它提供了一种简单的方式来构建和消费SOAP服务。下面将详细介绍如何利用XFire进行Web服务的开发。 **一、开发Web Services服务端** 1. **创建Web Services工程** 在Eclipse...

    XFire实现webServices源码

    在提供的“xFire实现webservices”项目中,你可以找到以下关键部分: 1. **服务接口类**:定义了Web服务的公共方法,通常以`interface`的形式存在。 2. **服务实现类**:实现了接口中的所有方法,是Web服务的实际...

    XFire-WebServices傻瓜配置.doc

    XFire-WebServices的配置过程相对简单,主要涉及接口定义、服务实现、服务配置和客户端调用。通过这种方式,开发者可以快速地构建和测试Web服务。同时,它还支持对远程Web服务的调用,使得集成第三方服务变得容易。...

    eclipse 使用xfire开发webservices server

    标题中的“eclipse使用xfire开发webservices server”意味着我们将使用Eclipse IDE和XFire来创建和部署一个Web服务服务器。以下是一步步的操作步骤: 1. **安装Eclipse和XFire插件**:首先确保你的Eclipse已经安装...

    XFIRE_WEBSERVICES实例

    在这个实例中,我们将深入探讨如何利用XFire构建和使用Web服务。 首先,我们需要理解Web服务的基本概念。Web服务是一种通过HTTP协议进行通信的应用程序,可以跨越不同的操作系统和编程语言提供服务。SOAP(Simple ...

    xfire webservices源码

    【xfire webservices源码】是一个用于理解和学习Web服务实现的开源项目,它基于XFire框架,该框架是Java世界中一个流行的SOAP和RESTful Web服务实现工具。XFire允许开发者快速、简单地构建和部署Web服务。在这个源码...

    XFire_demo.zip_XFire_demo_java webservices_webservices_xfire dem

    另一个文件名“webservices”可能是源代码或相关的配置文件,直接展示了如何用XFire构建和配置Web服务。 在这个XFire的演示中,我们可以预期学习以下知识点: 1. **Web服务基础**:理解Web服务的基本概念,如SOAP...

    MyEclipse开发的基于xfire的 webservices 最简单的demo

    【MyEclipse开发的基于Xfire的Web服务(Webservices)最简单DEMO详解】 在软件开发领域,Web服务是一种允许不同系统之间进行通信和数据交换的技术。它基于开放标准,如SOAP(Simple Object Access Protocol)和WSDL...

    使用Xfire构建JAVA的webService全过程(从服务端到客户端)

    总结,使用Xfire构建JAVA Web服务,服务端主要涉及服务接口的定义、服务类的实现、配置文件的设置以及服务的启动。客户端则通过WSDL文件生成代理类,调用相应方法与服务端通信。这种方式使得开发Web服务变得更加便捷...

    Xfire的client.zip_Xfire客户端代码_webservice

    9. **版本控制**:为了兼容不同版本的Web服务,客户端可能需要处理版本兼容问题,如WSDL(Web Services Description Language)版本的适配。 10. **日志记录**:客户端代码通常会包含日志记录功能,方便调试和问题...

    用xfire的Eclipse_Plugin生成web服务的客户端.

    1. **自动生成Web服务客户端代码**:这是插件的核心功能之一,通过解析WSDL(Web Services Description Language)文件,插件能够自动为指定的Web服务生成客户端存根代码,极大地简化了客户端开发过程。 2. **简化...

    java使用xfire创建和调用webservices

    Java 使用 XFire 创建和调用 WebServices 是一个常见的任务,特别是在构建分布式系统和服务导向架构(SOA)中。XFire 是一个 Java 框架,它简化了 WebService 的开发和消费过程。在这个主题中,我们将深入探讨如何...

    webservice xfire 客户端调用实现

    总的来说,XFire提供了一种简洁、高效的方式来实现Web服务的客户端调用,特别是其对对象传递的支持,使得开发者能够更加专注于业务逻辑,而不必过多关注底层的通信细节。在实际开发中,结合Spring等框架,可以构建出...

    java xfire Webservices实例

    Java XFire Web服务实例 Java XFire 是一个开源框架,它允许开发人员快速、轻松地创建和消费Web服务。在Java世界中,Web服务...通过深入学习和实践,你可以掌握Web服务的核心概念,并利用XFire构建自己的分布式系统。

    xfire整合spring发布web services

    这一步确保了我们拥有构建Web服务和管理应用程序上下文所需的基础组件。 接着,我们需要在`WEB-INF`目录下创建两个配置文件。`applicationContext.xml`用于定义项目中的Bean,而`xfire-servlet.xml`则专门用于配置...

    xfire开发web service文档.rar

    Web服务是一种基于互联网的软件应用程序接口(API)的交互...总的来说,这个压缩包资源对于任何想要深入了解使用XFire开发Web服务的Java开发者来说,都是一个宝贵的资料库,它提供了从零开始构建Web服务的全面指导。

    调用xfire接口的客户端实现

    在IT行业中,Web服务接口是不同系统间交互数据的重要方式,而XFire是早期流行的Java Web服务框架之一,用于创建和消费SOAP(简单对象访问协议)服务。本教程将详细介绍如何在客户端调用由XFire开发的Web服务接口,...

Global site tag (gtag.js) - Google Analytics