转自: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 {
- public int add(int p1, int p2);
- }
创建接口的实现类:
- package com.zx.ws.test;
- import com.zx.ws.test.TestService;
- public class TestServiceImp implements TestService{
- public int add(int p1, int p2)
- {
- return p1 + p2;
- }
- }
三.在META-INF下新建xfire文件夹,创建services.xml文件,配置如下:
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <span style="color: #ff0000;"><name>TestService</name>
- <namespace>http://com/zx/ws/test/TestService</namespace></span>
- <serviceClass>com.zx.ws.test.TestService</serviceClass>
- <implementationClass>com.zx.ws.test.TestServiceImp</implementationClass>
- </service>
- </beans>
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>webservice</display-name>
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>
- org.codehaus.xfire.transport.http.XFireConfigurableServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/servlet/XFireServlet/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </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,代码如下:
- package com.zx.ws.test;
- 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;
- import com.zx.ws.test.TestService;
- public class MyClient {
- public static void main(String[] args) {
- try
- {
- Service serviceModel = new ObjectServiceFactory() .create(TestService.class, "TestService", "http://com/zx/ws/test/TestService", 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目录下。