转自: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下面所需要的包。
?二.
?创建接口类:
- package?com.zx.ws.test;??
- ??
- public?interface?TestService?{??
- ????int??add(int?p1,?int?p2);??
- }??
?
创建接口的实现类:
import?com.zx.ws.test.TestService;??
class?TestServiceImp?implements?TestService{??
int?add(int?p2)??
????{??
return?p1?+?p2;??
????}??
?三.在META-INF下新建xfire文件夹,创建services.xml文件,配置如下:
<beans?xmlns="http://xfire.codehaus.org/config/1.0">??
- 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");??
??????
????????????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目录下。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!