1.下载支持XFire的Jar包.
      如果使用MyEclipse的话则不用下载,因为MyEclipse已经集成.在工程上右键选择MyEclipse->add WebService...进行添加,此处不做介绍.
      可以访问xfire.codehaus.org下载XFire框架的安装包,下载时请选择“全部二进制发布包(BinaryDistributioninzippackage)”,而不仅仅是“XFirejar文件(JarofallXFiremodules)”。下载完毕后解压,把根据目录下的xfire-all-1.2.6.jar和lib文件夹中的所有文件都拷贝到工程的WEB-INF/lib中.
2.配置XFire的运行环境.
      修改工程中的WEB-INF/web.xml文件,如下:
Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.      
  8.   <servlet>  
  9.     <servlet-name>XFireServlet</servlet-name>  
  10.     <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  
  11.     <load-on-startup>0</load-on-startup>  
  12.   </servlet>  
  13.   <servlet-mapping>  
  14.     <servlet-name>XFireServlet</servlet-name>  
  15.     <url-pattern>/services/*</url-pattern>  
  16.   </servlet-mapping>  
  17.      
  18.      
  19.   <welcome-file-list>  
  20.     <welcome-file>index.jsp</welcome-file>  
  21.   </welcome-file-list>  
  22. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
注意:此处可以在原来Web.xml文件上进行修改,只需求添加<servlet>和<servlet-mapping>标签即可,引入了XFireServlet类,以处理Web Service请求,并且负责提供Web Service的WSDL(Web服务描述语言)
3.在WEB-INF/classes下创建文件夹:/META-INF/xfire,并在其中创建services.xml文件,文件中内容如下:
Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://xfire.codehaus.org/config/1.0">  
  3.   
  4. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
</beans>
4.此时WebService的环境已经搭建完毕.
      在地址栏中输入http://localhost:8888/TestXFire/services,如果不报错说明配置成功.因为没有创建任何WebService所以显示如下信息:
          Generated by XFire ( http://xfire.codehaus.org )
5.创建WebService.
ISayHelloService.java文件如下:
Java代码
  1. package com.neusoft.study;   
  2.   
  3. public interface ISayHelloService   
  4. {   
  5.     public String sayHello(String name) ;   
  6. }  
package com.neusoft.study;
public interface ISayHelloService
{
    public String sayHello(String name) ;
}
      这个接口告诉服务器,你的WebService哪些方法是可以被用户调用的.下面再来写一个ISayHelloService的实现类,以完成业务逻辑.
SayHelloServiceImpl.java文件如下:
Java代码
  1. package com.neusoft.study;   
  2.   
  3. public class SayHelloServiceImpl implements ISayHelloService   
  4. {   
  5.   
  6.     public String sayHello(String name)   
  7.     {   
  8.         if(name==null || "".equals(name)){   
  9.             return "为什么不告诉我你的名字?" ;   
  10.         }   
  11.         return ",你妈喊你回家吃饭啦!";   
  12.     }   
  13.   
  14.     public String noSay(String name){   
  15.         return name+",不告诉你" ;   
  16.     }   
  17. }  
package com.neusoft.study;
public class SayHelloServiceImpl implements ISayHelloService
{
    public String sayHello(String name)
    {
        if(name==null || "".equals(name)){
            return "为什么不告诉我你的名字?" ;
        }
        return ",你妈喊你回家吃饭啦!";
    }
    public String noSay(String name){
        return name+",不告诉你" ;
    }
}
这个类实现了sayHello方法,这个方法是可以通过WebService访问到的.另外一个方法noSay(),该方法没有在接口ISayHelloService中定义,所以不能被WebService调用到.
6.发布WebService.
修改WEB-INF/classes/META-INF/xfire/services.xml文件,修改后的文件如下:
Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://xfire.codehaus.org/config/1.0">  
  3.     <service>  
  4.         <name>SayHelloService</name>  
  5.         <serviceClass>com.neusoft.study.ISayHelloService</serviceClass>  
  6.         <implementationClass>com.neusoft.study.SayHelloServiceImpl</implementationClass>  
  7.     </service>  
  8. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
        <name>SayHelloService</name>
        <serviceClass>com.neusoft.study.ISayHelloService</serviceClass>
        <implementationClass>com.neusoft.study.SayHelloServiceImpl</implementationClass>
    </service>
</beans>
这个文件定义了一个WebService:SayHelloService,并且同时定义了接口和实现类
7.启动Tomcat。
启动Tomcat,在浏览器地址栏中输入http://localhost:8888/TestXFire/services,会显示如下信息:
Available Services:
  • HelloWebService [wsdl]
    Generated by XFire ( http://xfire.codehaus.org )
点击[wsdl],会显示相应的WSDL信息。
8.测试WebService。
编写一个具有main函数的类,用于测试WebService.
Java代码 复制代码
  1. package com.zhang.main;   
  2.   
  3. import java.net.MalformedURLException;   
  4.   
  5. import org.codehaus.xfire.client.Client;   
  6. import org.codehaus.xfire.client.XFireProxyFactory;   
  7. import org.codehaus.xfire.service.Service;   
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
  9.   
  10. import com.zhang.ISayHelloService;   
  11.   
  12. public class SayHello   
  13. {   
  14.     /**  
  15.      * <p>Description:[方法功能描述]</p>  
  16.      * @param args  
  17.      * @author:{ysj}  
  18.      * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]  
  19.      */  
  20.   
  21.     public static void main(String[] args)   
  22.     {   
  23.         String serviceURL = "http://localhost:8888/TestXFire3/services/SayHelloService" ;//SayHelloService是在services.xml中配置的name标签的值   
  24.         Service serviceModel = new ObjectServiceFactory().create(ISayHelloService.classnull"http://localhost:8888/TestXFire3/services/SayHelloService?wsdl"null) ;//此处的URL为WSDL所在URL   
  25.         XFireProxyFactory serviceFactory = new XFireProxyFactory() ;//通过代理工厂创建一个service工厂   
  26.         try  
  27.         {   
  28.             ISayHelloService service = (ISayHelloService)serviceFactory.create(serviceModel, serviceURL) ;   
  29.             Client client = Client.getInstance(service) ;   
  30.             String hello = service.sayHello(null) ;   
  31.             System.out.println("服务器对无名氏说:"+hello) ;   
  32.             hello = service.sayHello("张三") ;   
  33.             System.out.println("服务器对[张三]说:"+hello) ;   
  34.         }   
  35.         catch (MalformedURLException e)   
  36.         {   
  37.             e.printStackTrace();   
  38.         }   
  39.     }   
  40. }  
package com.neusoft.main;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.neusoft.study.ISayHelloService;
public class SayHello
{
    /**
     * <p>Description:[方法功能描述]</p>
     * @param args
     * @author:{ysj}
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    public static void main(String[] args)
    {
        String serviceURL = "http://localhost:8888/TestXFire3/services/SayHelloService" ;//SayHelloService是在services.xml中配置的name标签的值
        Service serviceModel = new ObjectServiceFactory().create(ISayHelloService.class, null, "http://localhost:8888/TestXFire3/services/SayHelloService?wsdl", null) ;//此处的URL为WSDL所在URL
        XFireProxyFactory serviceFactory = new XFireProxyFactory() ;//通过代理工厂创建一个service工厂
        try
        {
            ISayHelloService service = (ISayHelloService)serviceFactory.create(serviceModel, serviceURL) ;
            Client client = Client.getInstance(service) ;
            String hello = service.sayHello(null) ;
            System.out.println("服务器对无名氏说:"+hello) ;
            hello = service.sayHello("张三") ;
            System.out.println("服务器对[张三]说:"+hello) ;
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
    }
}
输出结果为:
 
服务器对无名氏说:为什么不告诉我你的名字?
服务器对[张三]说:,你妈喊你回家吃饭啦!