使用xfire提供webService

今天稍稍研究了在web项目中集成xfire的方法
 
先注意以下内容
1. xfire官网上下载的例子,基本上不能用,因为xfire1.2.6认使用的是spring1.x的,到了2.x,找不到org.springframework.web.servlet这个包了。
 
2. 网上教的方法,大多都是照例子,很容易出错。
 
ok,下面进入正题,记录一下xfire的使用方法
 
从网站上下载到xfire发行版,约18M,建议使用发行版,里面包含了一些例子,更重要的是依赖包都放在lib目录下了,使得项目开发更方便。
 
myeclipse下,新建一个web项目(没有用web service project的方式,不太会用)。
导入以下依赖包:

 
除了spring.jar之外,都在xfire-1.2.6/lib目录下。
 
由于使用spring,因此在web.xml中添加以下内容
 
< web-app >

  <!-- Spring配置 -->        
   < context-param >

                 < param-name >contextConfigLocation </ param-name >
                 < param-value >classpath:org/codehaus/xfire/spring/xfire.xml,classpath:applicationContext.xml </ param-value >

         </ context-param >
        
   < listener >    
         < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class >    
   </ listener >        
    
        
  <!-- 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 >    

</ web-app >
 
先导入spring配置文件的路径,要注意的是,这里使用了xfire提供的配置文件,位于:org.codehaus.xfire.spring.xfire.xml。
然后就是spring的配置文件了,applicationContext.xml。
 
applicationContext.xml的配置很简洁。
   < bean id ="helloService" class ="sa.fs121.soap.service.impl.HelloServiceImpl" />

   < bean id ="getHelloService" class ="org.codehaus.xfire.spring.ServiceBean" >
     < property name ="serviceBean" ref ="helloService" />
     < property name ="serviceClass" value ="sa.fs121.soap.service.IHelloService" />

     < property name ="inHandlers" >
       < list >
         < ref bean ="addressingHandler" />
       </ list >
     </ property >
   </ bean >

   < bean id ="addressingHandler" class ="org.codehaus.xfire.addressing.AddressingInHandler" />
  
 
这个配置文件,细节方面还不是很了解,有待于深入下源码。
 
ok,还差业务代码
 
IHelloService.java
public interface IHelloService {

   public String sayHello(String hello);

}
 
实现类:
HelloServiceImpl.java
public class HelloServiceImpl implements IHelloService {

   public String sayHello(String hello) {
    System.out.println( "接收客户端请求:" + hello);
     return "Hello XFire," + hello;
  }

}
 
到这里,全部的代码就写完了。
结构如下:

 
启动tomcat服务器,访问: http://localhost:8080/xfire/service/?wsdl,正确的话,可以访问到wsdl。

 
使用Spring测试:
test.xml为配置文件
 
< beans >

   < bean id ="testWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientfactorybean" >
     < property name ="serviceClass" value ="sa.fs121.soap.service.IHelloService" />
     < property name ="wsdlDocumentUrl" value ="http://localhost:8080/xfire/service/IHelloService?wsdl" />
   </ bean >

</ beans >
 
测试代码
 
public static void main(String[] args) {
    ApplicationContext ctx = new ClasspathXmlApplicationContext( "test.xml");

    IHelloService hs = (IHelloService) ctx.getBean( "testWebService");

    System.out.println( "服务器响应:" + hs.sayHello( "hello,webservice"));
  }
 
搞定。

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...