标签:
XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,能够快速地开发Web Service应用。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成。
以下以一个Demo程序为例讲解如何编写一个Xfire的服务端与客户端:
xfire版本:xfire-distribution-1.2.6.zip
目录结构:
关键代码:
User.javapackage com.alfred.xfire.bean;
public class User {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
IHelloService.javapackage com.alfred.xfire.service;
import com.alfred.xfire.bean.User;
public interface IHelloService {
public String sayHello(String name);
public User getUser(User user);
}
HelloService.javapackage com.alfred.xfire.service;
import com.alfred.xfire.bean.User;
public class HelloService implements IHelloService {
public String sayHello(String name) {
return "Hello, " + name;
}
public User getUser(User user) {
User userNew = new User();
userNew.setId("next" + user.getId());
userNew.setName("next" + user.getName());
return userNew;
}
}
ClientTest.javapackage com.alfred.xfire.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFire;
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 com.alfred.xfire.bean.User;
import com.alfred.xfire.service.IHelloService;
public class ClientTest {
public static void main(String[] args) {
// visitMethod01();
visitMethod02();
}
private static void visitMethod01() {
try {
Client client = new Client(
new URL(
"http://127.0.0.1:8080/XfireDemo/services/HelloService?wsdl"));
Object[] results = client.invoke("sayHello",
new Object[] { "alfred" });
System.out.println(results[0].toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void visitMethod02() {
// Create a metadata of the service 创建一个service的元数据
Service srvcModel = new ObjectServiceFactory()
.create(IHelloService.class);
// Create a proxy for the deployed service 为XFire获得一个代理工厂那个对象
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String url = "http://127.0.0.1:8080/XfireDemo/services/HelloService";
try {
IHelloService helloService = (IHelloService) factory.create(
srvcModel, url);
User user = new User();
user.setId("userid");
user.setName("alfred");
User newUser = helloService.getUser(user);
System.out.println("id:" + newUser.getId());
System.out.println("name:" + newUser.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
services.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloService</name>
<namespace>com.alfred.xfire.service</namespace>
<serviceClass>com.alfred.xfire.service.IHelloService</serviceClass>
<implementationClass>com.alfred.xfire.service.HelloService</implementationClass>
</service>
</beans>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
导入jar包:
将xfire-all-1.2.6.jar以及lib目录下的jar包全部导入即可。
services.xml文件默认的存放目录是WEB-INF/META-INF/xfire下或WEB-INF/classes/META-INF/xfire下,如果是后者,那么实际项目中META-INF应放置于src目录下:
这样,部署到服务器中后META-INF文件夹就会存在于WEB-INF/classes/目录下。如果要自己指定services.xml文件路径,可以将services.xml路径配置在web.xml文件中,如下:
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
<!--
The servlet will by default look for the configuration on
the classpath in "META-INF/xfire/services.xml". You can
override it with this parameter. Seperate multiple configuration files with a comma.
-->
<!-- 默认会在classpath的META-INF/xfire/下查找services.xml文件,
可以覆盖这个参数,指定多个配置文件-->
<init-param>
<param-name>config</param-name>
<param-value>xfire-services.xml</param-value>
</init-param>
</servlet>
插入一段init-param配置,param-name为config,指定services.xml文件的路径以及文件名。
配置后目录结构如下:
private final static String CONFIG_FILE = "META-INF/xfire/services.xml";
configPath = getInitParameter(PARAM_CONFIG);
private final static String PARAM_CONFIG="config";
private String getConfigPath()
{
if (configPath == null || configPath.length() == 0)
{
return CONFIG_FILE;
}
return configPath;
}