加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

用xfire实现webservice

发布时间:2020-12-16 22:52:20 所属栏目:安全 来源:网络整理
导读:转自: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-


转自: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:

    • TestService?[wsdl]<!----><!---->


      或者输入http://localhost:8080/xfire/services/TestService?wsdl,如果正常显示wsdl文件的话就配置成功。

    客户端测试:

    首先把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目录下。

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读