一、在eclipse下新建一个web工程,名为:xfireDemo:
二、导入XFire用户库。该库中应包含xfire-1.26目录下的xfire-all-1.2.6.jar文件,以及xfire-1.2.6\lib目录下的所有文件。
三、修改项目下的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>xfireDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>/services/*</url-pattern>
</servlet-mapping>
</web-app>
web.xml中添加的servlet映射表明,所有匹配“/services/*”的url请求全部交给org.codehaus.xfire.transport.http.XFireConfigurableServlet来处理。
四、编写需要发布为WebService的Java接口:
package com.yao.xfire.service;
public interface MathService {
public int add(int a,int b);
}
五、编写实现其接口的类:
package com.yao.xfire.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MathServiceImpl implements MathService {
private final Log log = LogFactory.getLog(MathServiceImpl.class);
public int add(int a, int b) {
log.info("invoke method add.");
return a + b;
}
}
六、在WebContent\META-INF目录下新建xfire文件夹,然后在xfire目录下添加一个XFire使用的配置文件services.xml,该配置文件中的内容反映了要将哪些java类发布为web服务。本例中的services.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>MathService</name>
<namespace>http://com.yao.xfire.service/MathService</namespace>
<serviceClass>com.yao.xfire.service.MathService</serviceClass>
<implementationClass>com.yao.xfire.service.MathServiceImpl</implementationClass>
</service>
</beans>
XFire会借助Spring来解析services.xml,从中提取需要发布为WebService的配置信息。
很多文章介绍到这里就完了,然而当我按照他们所说的启动WebService ,然后通过http://localhost:8080/XFireZhuweiTest/services/MathService?wsdl 来访问服务描述时,却抛出了异常,说services.xml文件不存在--
“org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/xfire/services.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist”。
七、非常关键的一点,在WebContent\WEB-INF目录下新建classes文件夹,然后需要将WebContent下的整个META-INF文件夹剪切到新建的classes文件夹下。
到这里,项目的完整目录结构如下:
八、右键->Run As ->Run On Server,关联到你机器上的Tomcat,然后会启动Tomcat,以启动web服务。
九、在IE中输入 http://localhost:8080/xfireDemo/services/MathService?wsdl 会得到正确的web服务描述文档。
十、新建一个客户端访问类MathServiceClient:
package com.yao.xfire.client;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
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 org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
import com.yao.xfire.service.MathService;
public class MathServiceClient {
private static MathService mathService;
private MathServiceClient(){}
public static MathService getMathService(String serviceUrl) throws MalformedURLException{
if(mathService == null){
//创建服务
Service srvcModel = new ObjectServiceFactory().create(MathService.class);
//创建XFire对象
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//调用Web服务
mathService = (MathService) factory.create(srvcModel, serviceUrl);
//设置客户端调用的属性
Client client = Client.getInstance(mathService);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300");
client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
//如果需要设置代理
//client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "10.3.1.6" );
//client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "8080");
}
return mathService;
}
public static void main(String[] args) throws MalformedURLException{
MathService service = MathServiceClient.getMathService("http://localhost:8080/xfireDemo/services/MathService");
int result = service.add(1, 14);
System.out.println("get the result: " + result);
}
}
将得到结果:get the result: 15。
来源:oschina
链接:https://my.oschina.net/u/251220/blog/267081