XFire建立webservice服务和客户端

 

XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用。

相对于Axis来说,目前XFire相对受欢迎,加上其提供了和Spring集成的支持,在目前的Web Service开源社区拥有众多的追随者。并且因为XFire为Spring提供的支持,使得我们可以很容易在Spring中使用XFire构建Web Service应用。

XFire 与Axis2相比具有如下特征:

l 支持一系列Web Service的新标准--JSR181、WSDL2.0 、JAXB2、WS-Security等;

l 使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现;

l 容易上手,可以方便快速地从pojo发布服务;

l 支持Spring、Pico、Plexus、Loom等容器;

l 灵活的Binding机制,包括默认的Aegis,xmlbeans,jaxb2,castor;

l 高性能的SOAP 栈设计;

l 支持Spring、Pico、Plexus、Loom等容器。

XFire 与Axis1性能的比较如下:

l XFire 比Axis1.3快2-6倍;

l XFire 的响应时间是Axis1.3的1/2到1/5。

XFire 在WebService框架中开始较晚,它从现有的框架中借鉴了许多优秀的理念,力争将Web Service的应用开发难度降到最低。此外,还提供了各种绑定技术、支持多种传输协议,对WebService体系中许多新的规范提供了支持。

下面让我们来看一个XFire于Spring集成的helloWorld的简单例子。

一.实现的功能和特点

本例具有如下功能和特点:

1) 基于J2EE平台的Web Service服务;

2) 开发方便,配置简单;

3) 与spring无缝集成。

XFire 是完全基于流数据处理进行工作的系统,这意味着XFire不是将整个SOAP文档缓存在内存中,而是以管道的方式接收SOAP流数据。这种工作方式的转变带来了可观的性能回报,同时节省了内存的占用。

  XFire 从管道中接收一个SOAP请求到返回一个SOAP响应,会经历一系列的阶段。在管道调用的任何一个阶段,XFire都可以添加一些额外的Handler,在对消息进行加工处理后再传入到下一个阶段中。在SOAP请求消息对Web Service发起真正调用之前,分别会经过传输、预转发、转发、策略实施、用户信息处理、预调用、服务调用等阶段。

二.开发环境

笔者的开发环境描述如下:

1) jdk: 1.5 版本;

2) Tomcat :5.5.20;

3) MyEclipse: 5.1.1 GA 。

三.开发步骤

1. 工程与环境的建立

在 MyEclipse中新建Web工程,名为webservice_helloworld。选择该工程后,点击右键选择MyEclipse->Add Web Service Capabilities,弹出Add Web Service Capabilities对话框,点击“Next”,弹出Project Library Configuration对话框,默认选择Core Libraries,点击“Finish”按钮,完成XFire核心包的添加。为了后续的客户端的测试,还需读者加入commons- httpclient.jar包到WEB-INF/lib下。

部署后可看到此时WEB-INF/lib的jar包列表如下:

activation-1.1.jar 、 commons-beanutils-1.7.0.jar、commons-codec-1.3.jar、commons- httpclient.jar、commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jaxws-api- 2.0.jar、jdom-1.0.jar、jsr173_api-1.0.jar、mail-1.4.jar、saaj-api-1.3.jar、 saaj-impl-1.3.jar、spring-1.2.6.jar、stax-api-1.0.1.jar、wsdl4j-1.5.2.jar、 wstx-asl-3.0.1.jar、xbean-2.1.0.jar、xbean-spring-2.5.jar、xfire-aegis- 1.2.2.jar、xfire-annotations-1.2.2.jar、xfire-core-1.2.2.jar、xfire- java5-1.2.2.jar、xfire-jaxws-1.2.2.jar、xfire-jsr181-api-1.0-M1.jar、xfire- spring-1.2.2.jar、XmlSchema-1.1.jar

为了后续的开发和测试,在src目录下分别建立test和webservice目录,分别用于存放测试文件和webservice的相关类。

2 .Web Service实现的编写

在本例中,我们只是做一个helloWorld的简单例子。Web Service服务端提供一个根据输入的名字信息回复相应的helloWorld信息的。例如,当名字为“阿蜜果”时,恢复信息为“hello,阿蜜果”。下面让我们一步一步来开始进行编码。

1) web.xml 的配置

一般情况下,我们通过HTTP作为Web Service的传输协议,这样我们只需启动一个Web服务器(如Tomcat,在本例中使用的是Tomcat5.5.20),这样客户端就可以通过 HTTP访问到Web Service服务。为了集成Spring容器,XFire专门提供一个XFireSpringServlet,我们可以在web.xml中配置该 Servlet,将Spring容器中定义的Web Service在某个URI下发布。

为了能正确使用XFire,需在web.xml中进行相应配置,在该文件中配置XFire的 servlet 和 servlet-mapping 。同时因为本实例需要将 XFire 集成到 Spring 中,因而需要在 web.xml 文件中加载 Spring 的相应配置文件。在本实例中,我们首先在 WEB-INF 下建立两个配置 Spring 配置文件,一个为 applicationContext.xml ,该文件用来定义本工程的 bean ,一个为 xfire-servlet.xml ,用来配置 XFire 的相关 bean 。修改后的 web.xml 的内容如下所示:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. < web-app xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.4" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
  3. < display-name > XFireService </ display-name >
  4. <!-- begin Spring配置 -->
  5. < context-param >
  6. < param-name > contextConfigLocation </ param-name >
  7. < param-value > /WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml </ param-value >
  8. </ context-param >
  9. < listener >
  10. < listener-class > org.springframework.web.context.ContextLoaderListener
  11. </ listener-class >
  12. </ listener >
  13. < listener >
  14. < listener-class > org.springframework.web.util.IntrospectorCleanupListener
  15. </ listener-class >
  16. </ listener >
  17. <!-- end Spring配置 -->
  18. <!-- begin XFire 配置 -->
  19. < servlet >
  20. < servlet-name > xfire </ servlet-name >
  21. < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
  22. </ servlet >
  23. < servlet-mapping >
  24. < servlet-name > xfire </ servlet-name >
  25. < url-pattern > *.ws </ url-pattern >
  26. </ servlet-mapping >
  27. < servlet >
  28. <!-- 配合Spring容器中XFire一起工作的Servlet -->
  29. < servlet-name > xfireServlet </ servlet-name >
  30. < servlet-class > org.codehaus.xfire.spring.XFireSpringServlet </ servlet-class >
  31. </ servlet >
  32. < servlet-mapping >
  33. < servlet-name > xfireServlet </ servlet-name >
  34. <!-- 在这个URI下开放Web Service服务 -->
  35. < url-pattern > /service/* </ url-pattern >
  36. </ servlet-mapping >
  37. <!-- end XFire 配置 -->
  38. </ web-app >
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  xmlns ="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  version ="2.4"  xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
     < display-name > XFireService </ display-name >
     <!--  begin Spring配置  -->
     < context-param >
        < param-name > contextConfigLocation </ param-name >
        < param-value > /WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml </ param-value >
     </ context-param >
     < listener >
        < listener-class >  org.springframework.web.context.ContextLoaderListener
        </ listener-class >
     </ listener >
      < listener >  
        < listener-class >      org.springframework.web.util.IntrospectorCleanupListener
        </ listener-class >
     </ listener >
     <!--  end Spring配置  -->

     <!--  begin XFire 配置  -->
     < servlet >    
        < servlet-name > xfire </ servlet-name >    
        < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
     </ servlet >    
     < servlet-mapping >  
        < servlet-name > xfire </ servlet-name >
        < url-pattern > *.ws </ url-pattern >
     </ servlet-mapping >
     < servlet >
        <!--  配合Spring容器中XFire一起工作的Servlet -->
        < servlet-name > xfireServlet </ servlet-name >
        < servlet-class > org.codehaus.xfire.spring.XFireSpringServlet </ servlet-class >
     </ servlet >
     < servlet-mapping >
        < servlet-name > xfireServlet </ servlet-name >
        <!--  在这个URI下开放Web Service服务  -->
        < url-pattern > /service/* </ url-pattern >
     </ servlet-mapping >
     <!--  end XFire 配置  -->
</ web-app >

2) Web Service 的接口类 HelloWorld.java 和对应实现类 HelloWorldImpl.java

为了用 Web Service 完成 HelloWorld 功能,我们首先在 src/webservice 目录下建立接口类 HelloWold.java 。它仅包含一个 sayHelloWorld(String name) 的方法,其详细内容如下:
Java代码 复制代码 收藏代码
  1. package webservice;
  2. /** */ /**
  3. *HelloWorld的接口类.
  4. */
  5. public interface HelloWorld {
  6. /** */ /**
  7. *对名字为name的人打招呼.
  8. *@paramname名字
  9. *@return返回打招呼的字符串
  10. */
  11. String sayHelloWorld(String name);
  12. }
  13. 我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
  14. package webservice;
  15. /** */ /**
  16. *HelloWorld的实现类.
  17. */
  18. public class HelloWorldImpl implements HelloWorld {
  19. public String sayHelloWorld(String name) {
  20. String helloWorld = " hello, " + name;
  21. return helloWorld;
  22. }
  23. }
package  webservice;

/** */ /**
 *HelloWorld的接口类.
  */
public interface HelloWorld  {
     /** */ /**
     *对名字为name的人打招呼.
     *@paramname名字
     *@return返回打招呼的字符串
      */
    String sayHelloWorld(String name);
}
我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
package  webservice;
/** */ /**
 *HelloWorld的实现类.
  */
public class HelloWorldImpl  implements  HelloWorld  {
     public  String sayHelloWorld(String name)  {
       String helloWorld  =   " hello, "   +  name;
        return  helloWorld;
    }
}

3) Spring 配置文件 applicationContext.xml 和 xfire-servlet.xml 的配置

首先我们在 applicationContext.xml 文件中配置对应的 bean —— HelloWorldBean ,该 xml 文件的内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. < bean id ="HelloWorldBean" class ="webservice.HelloWorldImpl" />
  6. </ beans >
<? 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 ="HelloWorldBean"  class ="webservice.HelloWorldImpl" />
</ beans >

这个配置文件很简单,在此不详述。

XFire 为 Spring提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在Spring容器中将一个POJO导出为Web Service。HelloWorld是业务服务类,在此拥有一个sayHelloWorld的方法,我们希望将此方法开放为Web Service。在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为Web Service的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为Web Service的业务方法。

将一个业务类所有需要开放为Web Service的方法通过一个窄接口来描述是值得推荐的作法,这让Web Service的接口显得很“干净”。其次,XFire的导出器也需要服务接口的支持,因为它采用基于接口的动态代理技术。

窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据,为了简化实例,我们在此简化了实例。

下面让我们看看在xfire-servlet.xml文件中导出器的设置,该文件内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. <!-- 引入XFire预配置信息 -->
  6. < import resource ="classpath:org/codehaus/xfire/spring/xfire.xml" />
  7. <! —定义访问的url-- >
  8. < bean class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  9. < property name ="urlMap" >
  10. < map >
  11. < entry key ="/HelloWorldService.ws" >
  12. < ref bean ="HelloWorldService" />
  13. </ entry >
  14. </ map >
  15. </ property >
  16. </ bean >
  17. <!-- 使用XFire导出器 -->
  18. < bean id ="baseWebService" class ="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init ="false" abstract ="true" >
  19. <!-- 引用xfire.xml中定义的工厂 -->
  20. < property name ="serviceFactory" ref ="xfire.serviceFactory" />
  21. <!-- 引用xfire.xml中的xfire实例 -->
  22. < property name ="xfire" ref ="xfire" />
  23. </ bean >
  24. < bean id ="HelloWorldService" parent ="baseWebService" >
  25. <!-- 业务服务bean -->
  26. < property name ="serviceBean" ref ="HelloWorldBean" />
  27. <!-- 业务服务bean的窄接口类 -->
  28. < property name ="serviceClass" value ="webservice.HelloWorld" />
  29. </ bean >
  30. </ beans >
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
     <!--  引入XFire预配置信息  -->
     < import  resource ="classpath:org/codehaus/xfire/spring/xfire.xml"   />
     <! —定义访问的url-- >
     < bean  class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
        < property  name ="urlMap" >              
            < map >                  
               < entry  key ="/HelloWorldService.ws" >                   
                   < ref  bean ="HelloWorldService"   />                  
               </ entry >              
            </ map >          
        </ property >      
     </ bean >      

     <!--  使用XFire导出器  -->
     < bean  id ="baseWebService"  class ="org.codehaus.xfire.spring.remoting.XFireExporter"  lazy-init ="false"  abstract ="true" >
        <!--  引用xfire.xml中定义的工厂  -->
        < property  name ="serviceFactory"  ref ="xfire.serviceFactory"   />
        <!--  引用xfire.xml中的xfire实例  -->
        < property  name ="xfire"  ref ="xfire"   />
     </ bean >
     < bean  id ="HelloWorldService"  parent ="baseWebService" >
        <!--  业务服务bean  -->
        < property  name ="serviceBean"  ref ="HelloWorldBean"   />
        <!--  业务服务bean的窄接口类  -->
        < property  name ="serviceClass"  value ="webservice.HelloWorld"   />
     </ bean >
</ beans >

在上面的配置中,我们可以看到,在该配置文件中引入了xfire.xml这个Spring配置文件。它是在XFire核心JAR包中拥有一个预定义的 Spring配置文件,它定义了XFire在Spring中必须用到的一些Bean和资源,需要引入这个预定义的配置文件。从该配置文件中可以看出,我们通过XFireExporter将业务类导出为Web Service,对于任何导出器,我们都需要引入XFire环境,即serviceFactory 和 xfire ,这是标准的配置。 ServiceFactory 是XFire的核心类,它可以将一个POJO生成为一个Web Service。

在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire。

3. Web Service 的测试

在上一步操作完成之后,我们的这个简单的 Web Service 已经编写完毕,下面让我们来看看自己的劳动成果吧。

在浏览器中输入地址: http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl
,我们可以看到 HelloWorldService 对应的 WSDL 信息, 阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。

XFire 为访问服务端Web Service提供了各种方便的方式:我们一般根据服务地址和窄接口类创建客户调用程序。

  在不能获得服务窄接口类的情况下,XFire允许我们通过WSDL文件生成客户端调用程序,通过指定服务接口的方式调用服务。

1) 通过WSDL文件生成客户端调用程序

首先我们通过 http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl
我们可以获得 WSDL 文件 HelloWorldService.wsdl ,并将其放在 src 目录下面,接着我们通过程序访问该 WSDL 文件,并调用需测试的方法。此时测试类 WebServiceClientTest.java 的内容如下所示:
Java代码 复制代码 收藏代码
  1. package test;
  2. import org.codehaus.xfire.client.Client;
  3. import org.springframework.core.io.ClassPathResource;
  4. import org.springframework.core.io.Resource;
  5. import webservice.HelloWorld;
  6. /** */ /**
  7. *Copyright2007GuangZhouAmigo.
  8. *Allrightreserved.
  9. *HelloWorld的webservice的测试类.
  10. * @author <a href="mailto:[email protected]">AmigoXie</a>
  11. *@version1.0
  12. *Creationdate:2007-9-16-下午05:36:05
  13. */
  14. public class WebServiceClientTest {
  15. HelloWorld helloWorld = null ;
  16. public static void main(String[] args) throws Exception {
  17. WebServiceClientTest test = new WebServiceClientTest();
  18. test.testClient();
  19. }
  20. public void testClient() throws Exception {
  21. String wsdl = " HelloWorldService.wsdl " ; // 对应的WSDL文件
  22. Resource resource = new ClassPathResource(wsdl);
  23. Client client = new Client(resource.getInputStream(), null ); // 根据WSDL创建客户实例
  24. Object[] objArray = new Object[ 1 ];
  25. objArray[ 0 ] = " 阿蜜果 " ;
  26. // 调用特定的Web Service方法
  27. Object[] results = client.invoke( " sayHelloWorld " , objArray);
  28. System.out.println( " result: " + results[ 0 ]);
  29. }
  30. }
package  test;

import  org.codehaus.xfire.client.Client;
import  org.springframework.core.io.ClassPathResource;
import  org.springframework.core.io.Resource;
import  webservice.HelloWorld;

/** */ /**
 *Copyright2007GuangZhouAmigo.
 *Allrightreserved.   
 *HelloWorld的webservice的测试类.
 * @author <a href="mailto:[email protected]">AmigoXie</a>
 *@version1.0
 *Creationdate:2007-9-16-下午05:36:05
  */
public class WebServiceClientTest  { 
    HelloWorld helloWorld  =   null ;

    public static void main(String[] args)  throws  Exception  {
       WebServiceClientTest test  =   new  WebServiceClientTest();
       test.testClient();
    }  
    
    public void testClient()  throws  Exception  {
       String wsdl  =   " HelloWorldService.wsdl " ;  // 对应的WSDL文件
       Resource resource  =   new  ClassPathResource(wsdl); 
       Client client  =   new  Client(resource.getInputStream(),  null );  // 根据WSDL创建客户实例
       
       Object[] objArray  =   new  Object[ 1 ];
       objArray[ 0 ]  =   " 阿蜜果 " ;
        // 调用特定的Web Service方法
       Object[] results  =  client.invoke( " sayHelloWorld " , objArray);
       System.out.println( " result:  "   +  results[ 0 ]);
    }
} 

运行该类,可得到如下输出结果:

result: hello, 阿蜜果

可看出运行结果正确。

2 )根据服务地址创建客户端调用程序

接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立 WebServiceClientTest.java 文件,并在 src 目录下建立客户端调用的 Spring 配置文件 client.xml 。在 client.xml 配置文件中我们定义了一个 testWebService 的 bean ,该 bean 访问 wsdlDocumentUrl 为 http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的 WSDL 。该 xml 文件的详细内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. < bean id ="testWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" >
  6. < property name ="serviceClass" >
  7. < value > webservice.HelloWorld </ value >
  8. </ property >
  9. < property name ="wsdlDocumentUrl" >
  10. < value > http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl </ value >
  11. </ property >
  12. </ bean >
  13. </ beans >
<? 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 ="testWebService"  class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" >
        < property  name ="serviceClass" >           
    < value > webservice.HelloWorld </ value >        
        </ property >       
         < property  name ="wsdlDocumentUrl" >          
< value > http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl </ value >        
         </ property >       
      </ bean >
</ beans >

在 WebServiceClientTest.java 文件中获得 HelloWorld ,并调用它的 sayHelloWorld 方法来完成测试,该类的详细内容如下所示:
Java代码 复制代码 收藏代码
  1. package test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import webservice.HelloWorld;
  5. /** */ /**
  6. *HelloWorld的webservice的测试类.
  7. */
  8. public class WebServiceClientTest {
  9. HelloWorld helloWorld = null ;
  10. public static void main(String[] args) {
  11. WebServiceClientTest test = new WebServiceClientTest();
  12. test.testClient();
  13. }
  14. public void testClient() {
  15. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  16. " client.xml " );
  17. helloWorld = (HelloWorld) ctx.getBean( " testWebService " );
  18. System.out.println(helloWorld.sayHelloWorld( " 阿蜜果 " ));
  19. }
  20. }
package  test;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  webservice.HelloWorld;

/** */ /**
 *HelloWorld的webservice的测试类.
  */
public class WebServiceClientTest  { 
    HelloWorld helloWorld  =   null ;

    public static void main(String[] args)  {
       WebServiceClientTest test  =   new  WebServiceClientTest();
       test.testClient();
    }  

    public void testClient()  {
       ApplicationContext ctx  =   new  ClassPathXmlApplicationContext(
               " client.xml " );
       helloWorld  =  (HelloWorld) ctx.getBean( " testWebService " );
       System.out.println(helloWorld.sayHelloWorld( " 阿蜜果 " ));
    }
}

在启动 webservice_helloworld 工程的情况下,运行 WebServiceClientTest 类,可看到控制台包含如下信息:

hello, 阿蜜果

由此可看出调用 Web Service 成功。

四.总结

与 Axis相比,在实施Web Service时XFire更加简洁高效,并且XFire对Spring提供了强大的支持,可以非常方便地在Spring中使用XFire实施Web Service,因此XFire在短短的时间里成为了受Web Service开发者喜爱的框架。

  XFire为客户端提供了多种访问Web Service的方式,如果可以获取客户端的窄接口类,则可以采用窄接口类调用Web Service。如果仅能获取WSDL,XFire也可以采用动态反射的机制调用Web Service。XFire为Eclipse提供了一个可以根据WSDL生成客户端存根代码的插件,相信XFire也将为其它非Java语言提供类似的插件。

技术可用性的一个很大的标准是它是否方便测试,XFire提供多种方式进行Web Service的测试,简单方便,给Web Service开发人员的测试工作带来了福音。
XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用。

相对于Axis来说,目前XFire相对受欢迎,加上其提供了和Spring集成的支持,在目前的Web Service开源社区拥有众多的追随者。并且因为XFire为Spring提供的支持,使得我们可以很容易在Spring中使用XFire构建Web Service应用。

XFire 与Axis2相比具有如下特征:

l 支持一系列Web Service的新标准--JSR181、WSDL2.0 、JAXB2、WS-Security等;

l 使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现;

l 容易上手,可以方便快速地从pojo发布服务;

l 支持Spring、Pico、Plexus、Loom等容器;

l 灵活的Binding机制,包括默认的Aegis,xmlbeans,jaxb2,castor;

l 高性能的SOAP 栈设计;

l 支持Spring、Pico、Plexus、Loom等容器。

XFire 与Axis1性能的比较如下:

l XFire 比Axis1.3快2-6倍;

l XFire 的响应时间是Axis1.3的1/2到1/5。

XFire 在WebService框架中开始较晚,它从现有的框架中借鉴了许多优秀的理念,力争将Web Service的应用开发难度降到最低。此外,还提供了各种绑定技术、支持多种传输协议,对WebService体系中许多新的规范提供了支持。

下面让我们来看一个XFire于Spring集成的helloWorld的简单例子。

一.实现的功能和特点

本例具有如下功能和特点:

1) 基于J2EE平台的Web Service服务;

2) 开发方便,配置简单;

3) 与spring无缝集成。

XFire 是完全基于流数据处理进行工作的系统,这意味着XFire不是将整个SOAP文档缓存在内存中,而是以管道的方式接收SOAP流数据。这种工作方式的转变带来了可观的性能回报,同时节省了内存的占用。

  XFire 从管道中接收一个SOAP请求到返回一个SOAP响应,会经历一系列的阶段。在管道调用的任何一个阶段,XFire都可以添加一些额外的Handler,在对消息进行加工处理后再传入到下一个阶段中。在SOAP请求消息对Web Service发起真正调用之前,分别会经过传输、预转发、转发、策略实施、用户信息处理、预调用、服务调用等阶段。

二.开发环境

笔者的开发环境描述如下:

1) jdk: 1.5 版本;

2) Tomcat :5.5.20;

3) MyEclipse: 5.1.1 GA 。

三.开发步骤

1. 工程与环境的建立

在 MyEclipse中新建Web工程,名为webservice_helloworld。选择该工程后,点击右键选择MyEclipse->Add Web Service Capabilities,弹出Add Web Service Capabilities对话框,点击“Next”,弹出Project Library Configuration对话框,默认选择Core Libraries,点击“Finish”按钮,完成XFire核心包的添加。为了后续的客户端的测试,还需读者加入commons- httpclient.jar包到WEB-INF/lib下。

部署后可看到此时WEB-INF/lib的jar包列表如下:

activation-1.1.jar 、 commons-beanutils-1.7.0.jar、commons-codec-1.3.jar、commons- httpclient.jar、commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jaxws-api- 2.0.jar、jdom-1.0.jar、jsr173_api-1.0.jar、mail-1.4.jar、saaj-api-1.3.jar、 saaj-impl-1.3.jar、spring-1.2.6.jar、stax-api-1.0.1.jar、wsdl4j-1.5.2.jar、 wstx-asl-3.0.1.jar、xbean-2.1.0.jar、xbean-spring-2.5.jar、xfire-aegis- 1.2.2.jar、xfire-annotations-1.2.2.jar、xfire-core-1.2.2.jar、xfire- java5-1.2.2.jar、xfire-jaxws-1.2.2.jar、xfire-jsr181-api-1.0-M1.jar、xfire- spring-1.2.2.jar、XmlSchema-1.1.jar

为了后续的开发和测试,在src目录下分别建立test和webservice目录,分别用于存放测试文件和webservice的相关类。

2 .Web Service实现的编写

在本例中,我们只是做一个helloWorld的简单例子。Web Service服务端提供一个根据输入的名字信息回复相应的helloWorld信息的。例如,当名字为“阿蜜果”时,恢复信息为“hello,阿蜜果”。下面让我们一步一步来开始进行编码。

1) web.xml 的配置

一般情况下,我们通过HTTP作为Web Service的传输协议,这样我们只需启动一个Web服务器(如Tomcat,在本例中使用的是Tomcat5.5.20),这样客户端就可以通过 HTTP访问到Web Service服务。为了集成Spring容器,XFire专门提供一个XFireSpringServlet,我们可以在web.xml中配置该 Servlet,将Spring容器中定义的Web Service在某个URI下发布。

为了能正确使用XFire,需在web.xml中进行相应配置,在该文件中配置XFire的 servlet 和 servlet-mapping 。同时因为本实例需要将 XFire 集成到 Spring 中,因而需要在 web.xml 文件中加载 Spring 的相应配置文件。在本实例中,我们首先在 WEB-INF 下建立两个配置 Spring 配置文件,一个为 applicationContext.xml ,该文件用来定义本工程的 bean ,一个为 xfire-servlet.xml ,用来配置 XFire 的相关 bean 。修改后的 web.xml 的内容如下所示:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. < web-app xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.4" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
  3. < display-name > XFireService </ display-name >
  4. <!-- begin Spring配置 -->
  5. < context-param >
  6. < param-name > contextConfigLocation </ param-name >
  7. < param-value > /WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml </ param-value >
  8. </ context-param >
  9. < listener >
  10. < listener-class > org.springframework.web.context.ContextLoaderListener
  11. </ listener-class >
  12. </ listener >
  13. < listener >
  14. < listener-class > org.springframework.web.util.IntrospectorCleanupListener
  15. </ listener-class >
  16. </ listener >
  17. <!-- end Spring配置 -->
  18. <!-- begin XFire 配置 -->
  19. < servlet >
  20. < servlet-name > xfire </ servlet-name >
  21. < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
  22. </ servlet >
  23. < servlet-mapping >
  24. < servlet-name > xfire </ servlet-name >
  25. < url-pattern > *.ws </ url-pattern >
  26. </ servlet-mapping >
  27. < servlet >
  28. <!-- 配合Spring容器中XFire一起工作的Servlet -->
  29. < servlet-name > xfireServlet </ servlet-name >
  30. < servlet-class > org.codehaus.xfire.spring.XFireSpringServlet </ servlet-class >
  31. </ servlet >
  32. < servlet-mapping >
  33. < servlet-name > xfireServlet </ servlet-name >
  34. <!-- 在这个URI下开放Web Service服务 -->
  35. < url-pattern > /service/* </ url-pattern >
  36. </ servlet-mapping >
  37. <!-- end XFire 配置 -->
  38. </ web-app >
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  xmlns ="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  version ="2.4"  xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
     < display-name > XFireService </ display-name >
     <!--  begin Spring配置  -->
     < context-param >
        < param-name > contextConfigLocation </ param-name >
        < param-value > /WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml </ param-value >
     </ context-param >
     < listener >
        < listener-class >  org.springframework.web.context.ContextLoaderListener
        </ listener-class >
     </ listener >
      < listener >  
        < listener-class >      org.springframework.web.util.IntrospectorCleanupListener
        </ listener-class >
     </ listener >
     <!--  end Spring配置  -->

     <!--  begin XFire 配置  -->
     < servlet >    
        < servlet-name > xfire </ servlet-name >    
        < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
     </ servlet >    
     < servlet-mapping >  
        < servlet-name > xfire </ servlet-name >
        < url-pattern > *.ws </ url-pattern >
     </ servlet-mapping >
     < servlet >
        <!--  配合Spring容器中XFire一起工作的Servlet -->
        < servlet-name > xfireServlet </ servlet-name >
        < servlet-class > org.codehaus.xfire.spring.XFireSpringServlet </ servlet-class >
     </ servlet >
     < servlet-mapping >
        < servlet-name > xfireServlet </ servlet-name >
        <!--  在这个URI下开放Web Service服务  -->
        < url-pattern > /service/* </ url-pattern >
     </ servlet-mapping >
     <!--  end XFire 配置  -->
</ web-app >

2) Web Service 的接口类 HelloWorld.java 和对应实现类 HelloWorldImpl.java

为了用 Web Service 完成 HelloWorld 功能,我们首先在 src/webservice 目录下建立接口类 HelloWold.java 。它仅包含一个 sayHelloWorld(String name) 的方法,其详细内容如下:
Java代码 复制代码 收藏代码
  1. package webservice;
  2. /** */ /**
  3. *HelloWorld的接口类.
  4. */
  5. public interface HelloWorld {
  6. /** */ /**
  7. *对名字为name的人打招呼.
  8. *@paramname名字
  9. *@return返回打招呼的字符串
  10. */
  11. String sayHelloWorld(String name);
  12. }
  13. 我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
  14. package webservice;
  15. /** */ /**
  16. *HelloWorld的实现类.
  17. */
  18. public class HelloWorldImpl implements HelloWorld {
  19. public String sayHelloWorld(String name) {
  20. String helloWorld = " hello, " + name;
  21. return helloWorld;
  22. }
  23. }
package  webservice;

/** */ /**
 *HelloWorld的接口类.
  */
public interface HelloWorld  {
     /** */ /**
     *对名字为name的人打招呼.
     *@paramname名字
     *@return返回打招呼的字符串
      */
    String sayHelloWorld(String name);
}
我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
package  webservice;
/** */ /**
 *HelloWorld的实现类.
  */
public class HelloWorldImpl  implements  HelloWorld  {
     public  String sayHelloWorld(String name)  {
       String helloWorld  =   " hello, "   +  name;
        return  helloWorld;
    }
}

3) Spring 配置文件 applicationContext.xml 和 xfire-servlet.xml 的配置

首先我们在 applicationContext.xml 文件中配置对应的 bean —— HelloWorldBean ,该 xml 文件的内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. < bean id ="HelloWorldBean" class ="webservice.HelloWorldImpl" />
  6. </ beans >
<? 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 ="HelloWorldBean"  class ="webservice.HelloWorldImpl" />
</ beans >

这个配置文件很简单,在此不详述。

XFire 为 Spring提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在Spring容器中将一个POJO导出为Web Service。HelloWorld是业务服务类,在此拥有一个sayHelloWorld的方法,我们希望将此方法开放为Web Service。在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为Web Service的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为Web Service的业务方法。

将一个业务类所有需要开放为Web Service的方法通过一个窄接口来描述是值得推荐的作法,这让Web Service的接口显得很“干净”。其次,XFire的导出器也需要服务接口的支持,因为它采用基于接口的动态代理技术。

窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据,为了简化实例,我们在此简化了实例。

下面让我们看看在xfire-servlet.xml文件中导出器的设置,该文件内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. <!-- 引入XFire预配置信息 -->
  6. < import resource ="classpath:org/codehaus/xfire/spring/xfire.xml" />
  7. <! —定义访问的url-- >
  8. < bean class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  9. < property name ="urlMap" >
  10. < map >
  11. < entry key ="/HelloWorldService.ws" >
  12. < ref bean ="HelloWorldService" />
  13. </ entry >
  14. </ map >
  15. </ property >
  16. </ bean >
  17. <!-- 使用XFire导出器 -->
  18. < bean id ="baseWebService" class ="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init ="false" abstract ="true" >
  19. <!-- 引用xfire.xml中定义的工厂 -->
  20. < property name ="serviceFactory" ref ="xfire.serviceFactory" />
  21. <!-- 引用xfire.xml中的xfire实例 -->
  22. < property name ="xfire" ref ="xfire" />
  23. </ bean >
  24. < bean id ="HelloWorldService" parent ="baseWebService" >
  25. <!-- 业务服务bean -->
  26. < property name ="serviceBean" ref ="HelloWorldBean" />
  27. <!-- 业务服务bean的窄接口类 -->
  28. < property name ="serviceClass" value ="webservice.HelloWorld" />
  29. </ bean >
  30. </ beans >
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
     <!--  引入XFire预配置信息  -->
     < import  resource ="classpath:org/codehaus/xfire/spring/xfire.xml"   />
     <! —定义访问的url-- >
     < bean  class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
        < property  name ="urlMap" >              
            < map >                  
               < entry  key ="/HelloWorldService.ws" >                   
                   < ref  bean ="HelloWorldService"   />                  
               </ entry >              
            </ map >          
        </ property >      
     </ bean >      

     <!--  使用XFire导出器  -->
     < bean  id ="baseWebService"  class ="org.codehaus.xfire.spring.remoting.XFireExporter"  lazy-init ="false"  abstract ="true" >
        <!--  引用xfire.xml中定义的工厂  -->
        < property  name ="serviceFactory"  ref ="xfire.serviceFactory"   />
        <!--  引用xfire.xml中的xfire实例  -->
        < property  name ="xfire"  ref ="xfire"   />
     </ bean >
     < bean  id ="HelloWorldService"  parent ="baseWebService" >
        <!--  业务服务bean  -->
        < property  name ="serviceBean"  ref ="HelloWorldBean"   />
        <!--  业务服务bean的窄接口类  -->
        < property  name ="serviceClass"  value ="webservice.HelloWorld"   />
     </ bean >
</ beans >

在上面的配置中,我们可以看到,在该配置文件中引入了xfire.xml这个Spring配置文件。它是在XFire核心JAR包中拥有一个预定义的 Spring配置文件,它定义了XFire在Spring中必须用到的一些Bean和资源,需要引入这个预定义的配置文件。从该配置文件中可以看出,我们通过XFireExporter将业务类导出为Web Service,对于任何导出器,我们都需要引入XFire环境,即serviceFactory 和 xfire ,这是标准的配置。 ServiceFactory 是XFire的核心类,它可以将一个POJO生成为一个Web Service。

在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire。

3. Web Service 的测试

在上一步操作完成之后,我们的这个简单的 Web Service 已经编写完毕,下面让我们来看看自己的劳动成果吧。

在浏览器中输入地址: http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl
,我们可以看到 HelloWorldService 对应的 WSDL 信息, 阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。

XFire 为访问服务端Web Service提供了各种方便的方式:我们一般根据服务地址和窄接口类创建客户调用程序。

  在不能获得服务窄接口类的情况下,XFire允许我们通过WSDL文件生成客户端调用程序,通过指定服务接口的方式调用服务。

1) 通过WSDL文件生成客户端调用程序

首先我们通过 http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl
我们可以获得 WSDL 文件 HelloWorldService.wsdl ,并将其放在 src 目录下面,接着我们通过程序访问该 WSDL 文件,并调用需测试的方法。此时测试类 WebServiceClientTest.java 的内容如下所示:
Java代码 复制代码 收藏代码
  1. package test;
  2. import org.codehaus.xfire.client.Client;
  3. import org.springframework.core.io.ClassPathResource;
  4. import org.springframework.core.io.Resource;
  5. import webservice.HelloWorld;
  6. /** */ /**
  7. *Copyright2007GuangZhouAmigo.
  8. *Allrightreserved.
  9. *HelloWorld的webservice的测试类.
  10. * @author <a href="mailto:[email protected]">AmigoXie</a>
  11. *@version1.0
  12. *Creationdate:2007-9-16-下午05:36:05
  13. */
  14. public class WebServiceClientTest {
  15. HelloWorld helloWorld = null ;
  16. public static void main(String[] args) throws Exception {
  17. WebServiceClientTest test = new WebServiceClientTest();
  18. test.testClient();
  19. }
  20. public void testClient() throws Exception {
  21. String wsdl = " HelloWorldService.wsdl " ; // 对应的WSDL文件
  22. Resource resource = new ClassPathResource(wsdl);
  23. Client client = new Client(resource.getInputStream(), null ); // 根据WSDL创建客户实例
  24. Object[] objArray = new Object[ 1 ];
  25. objArray[ 0 ] = " 阿蜜果 " ;
  26. // 调用特定的Web Service方法
  27. Object[] results = client.invoke( " sayHelloWorld " , objArray);
  28. System.out.println( " result: " + results[ 0 ]);
  29. }
  30. }
package  test;

import  org.codehaus.xfire.client.Client;
import  org.springframework.core.io.ClassPathResource;
import  org.springframework.core.io.Resource;
import  webservice.HelloWorld;

/** */ /**
 *Copyright2007GuangZhouAmigo.
 *Allrightreserved.   
 *HelloWorld的webservice的测试类.
 * @author <a href="mailto:[email protected]">AmigoXie</a>
 *@version1.0
 *Creationdate:2007-9-16-下午05:36:05
  */
public class WebServiceClientTest  { 
    HelloWorld helloWorld  =   null ;

    public static void main(String[] args)  throws  Exception  {
       WebServiceClientTest test  =   new  WebServiceClientTest();
       test.testClient();
    }  
    
    public void testClient()  throws  Exception  {
       String wsdl  =   " HelloWorldService.wsdl " ;  // 对应的WSDL文件
       Resource resource  =   new  ClassPathResource(wsdl); 
       Client client  =   new  Client(resource.getInputStream(),  null );  // 根据WSDL创建客户实例
       
       Object[] objArray  =   new  Object[ 1 ];
       objArray[ 0 ]  =   " 阿蜜果 " ;
        // 调用特定的Web Service方法
       Object[] results  =  client.invoke( " sayHelloWorld " , objArray);
       System.out.println( " result:  "   +  results[ 0 ]);
    }
} 

运行该类,可得到如下输出结果:

result: hello, 阿蜜果

可看出运行结果正确。

2 )根据服务地址创建客户端调用程序

接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立 WebServiceClientTest.java 文件,并在 src 目录下建立客户端调用的 Spring 配置文件 client.xml 。在 client.xml 配置文件中我们定义了一个 testWebService 的 bean ,该 bean 访问 wsdlDocumentUrl 为 http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的 WSDL 。该 xml 文件的详细内容如下:
Xml代码 复制代码 收藏代码
  1. <? xml version="1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd" >
  4. < beans >
  5. < bean id ="testWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" >
  6. < property name ="serviceClass" >
  7. < value > webservice.HelloWorld </ value >
  8. </ property >
  9. < property name ="wsdlDocumentUrl" >
  10. < value > http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl </ value >
  11. </ property >
  12. </ bean >
  13. </ beans >
<? 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 ="testWebService"  class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" >
        < property  name ="serviceClass" >           
    < value > webservice.HelloWorld </ value >        
        </ property >       
         < property  name ="wsdlDocumentUrl" >          
< value > http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl </ value >        
         </ property >       
      </ bean >
</ beans >

在 WebServiceClientTest.java 文件中获得 HelloWorld ,并调用它的 sayHelloWorld 方法来完成测试,该类的详细内容如下所示:
Java代码 复制代码 收藏代码
  1. package test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import webservice.HelloWorld;
  5. /** */ /**
  6. *HelloWorld的webservice的测试类.
  7. */
  8. public class WebServiceClientTest {
  9. HelloWorld helloWorld = null ;
  10. public static void main(String[] args) {
  11. WebServiceClientTest test = new WebServiceClientTest();
  12. test.testClient();
  13. }
  14. public void testClient() {
  15. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  16. " client.xml " );
  17. helloWorld = (HelloWorld) ctx.getBean( " testWebService " );
  18. System.out.println(helloWorld.sayHelloWorld( " 阿蜜果 " ));
  19. }
  20. }
package  test;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  webservice.HelloWorld;

/** */ /**
 *HelloWorld的webservice的测试类.
  */
public class WebServiceClientTest  { 
    HelloWorld helloWorld  =   null ;

    public static void main(String[] args)  {
       WebServiceClientTest test  =   new  WebServiceClientTest();
       test.testClient();
    }  

    public void testClient()  {
       ApplicationContext ctx  =   new  ClassPathXmlApplicationContext(
               " client.xml " );
       helloWorld  =  (HelloWorld) ctx.getBean( " testWebService " );
       System.out.println(helloWorld.sayHelloWorld( " 阿蜜果 " ));
    }
}

在启动 webservice_helloworld 工程的情况下,运行 WebServiceClientTest 类,可看到控制台包含如下信息:

hello, 阿蜜果

由此可看出调用 Web Service 成功。

四.总结

与 Axis相比,在实施Web Service时XFire更加简洁高效,并且XFire对Spring提供了强大的支持,可以非常方便地在Spring中使用XFire实施Web Service,因此XFire在短短的时间里成为了受Web Service开发者喜爱的框架。

  XFire为客户端提供了多种访问Web Service的方式,如果可以获取客户端的窄接口类,则可以采用窄接口类调用Web Service。如果仅能获取WSDL,XFire也可以采用动态反射的机制调用Web Service。XFire为Eclipse提供了一个可以根据WSDL生成客户端存根代码的插件,相信XFire也将为其它非Java语言提供类似的插件。

技术可用性的一个很大的标准是它是否方便测试,XFire提供多种方式进行Web Service的测试,简单方便,给Web Service开发人员的测试工作带来了福音。

猜你喜欢

转载自premier9527.iteye.com/blog/1663626