微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用xfire实现webservice


转自:http://rargers.iteye.com/blog/195503

也可以参考:http://blog.sina.com.cn/s/blog_477252210100a7l4.html


首先下载xfire包,目前最新的是1.2.6, 
下载地址:http://xfire.codehaus.org/Download 
一.新建一个web工程,取名为xfire,到入xfire-all-1.2.6.jar和lib下面所需要的包。

 二.

 创建接口类:

Java代码  

收藏代码

  1. package com.zx.ws.test;  
  2.   
  3. public interface TestService {  
  4.     int  add(int p1, int p2);  
  5. }  

 

创建接口的实现类:

import com.zx.ws.test.TestService;  
  • class TestServiceImp implements TestService{  
  • int add(int p2)  
  •     {  
  • return p1 + p2;  
  •     }  
  •  三.在meta-inf下新建xfire文件夹,创建services.xml文件,配置如下:

    Xml代码  

    收藏代码

      <beans xmlns="http://xfire.codehaus.org/config/1.0">  
    1. servicespan style="color: #ff0000;">name>TestService</namespace>http://com/zx/ws/test/TestServicespanserviceClass>com.zx.ws.test.TestServiceimplementationClass>com.zx.ws.test.TestServiceImpbeans>  

    web.xml 配置如下:

    <?xml version="1.0" encoding="UTF-8"?>  
  • web-app id="WebApp_ID" 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"display-name>webserviceservletservlet-name>XFireServletservlet-class org.codehaus.xfire.transport.http.XFireConfigurableServlet  
  • servlet-mappingurl-pattern>/servlet/XFireServlet/*>/services/*web-app   

    启动tomcat,然后在浏览器中输入http://localhost:8080/xfire/services/,如果显示

    Available Services:

    客户端测试:

    首先把xfire工程的打成jar包

    新建web动态工程,倒入xfire的包,和刚才的xfire工程的打包。

    新建一个测试类,MyClient,代码如下:

    import java.net.MalformedURLException;   
  • import java.rmi.remoteexception;  
  • import org.codehaus.xfire.client.XFireProxyFactory;   
  • import org.codehaus.xfire.service.Service;  
  • import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  • import com.zx.ws.test.TestServiceImp;  
  • class MyClient {  
  • static void main(String[] args) {   
  •         try   
  •         {   
  •               
  •             Service serviceModel = new ObjectServiceFactory() .create(TestService.class"TestService""http://com/zx/ws/test/TestService",85); font-weight:bold">null);  
  •             TestService service = (TestService) new XFireProxyFactory().create( serviceModel, "http://localhost:8080/xfire/services/TestService");  
  •     //使用 XFire 与 Spring 开发 Web Service 11  
  •             System.out.println("返回值是"+service.add(12,12));  
  •     }   
  • catch (MalformedURLException e) {   
  •             e.printstacktrace();   
  •             }   
  • }   
  •           
  •  然后运行,如果控制台输出

    返回值是24

    则说明客户端成功。

    ps:如果出现class path resource [meta-inf/xfire/services.xml] cannot be opened because it does not exist

    修改方法:把meta-inf文件copy到工程的build的classes目录下。

  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐