最近一个项目中需要用到Webservice向客户端提供各种服务。最初在选Webservice实现上考虑过AXIS1、XFire和CXF。尽管AXIS1技术成熟文档全面,但考虑其部署繁琐维护成本较高最终放弃。在看到XFire官方的一段说明后,初步决定使用CXF。
XFire官方说明
User's looking to use XFire on a new project, should use CXF instead. CXF is a continuation of the XFire project and is considered XFire 2.0. It has many new features, a ton of bug fixes, and is now JAX-WS compliant! XFire will continue to be maintained through bug fix releases, but most development will occur on CXF now. For more information see the XFire/Celtix merge FAQ and the CXF website .
后来由于客户提供的软件运行环境为Websphere6.0,而CXF需要JDK 5.0支持而不得不放弃,最后选择了XFire。总得来说XFire支持的特性广泛、配置部署简单,另外与Spring结合也相当方便,XFire也算是个不错的选择。
XFire之Hello World
1 创建一个Web工程,导入XFire所需jar包
2 配置web.xml
<!-- XFIRE配置 --> <servlet> <servlet-name>XFireServlet</servlet-name> <display-name>XFire Servlet</display-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>
3 在src下创建META-INF子目录,再在META-INF目录下创建xfire子目录,最后在xfire目录下创建services.xml文件
4 编写服务类接口HiService和实现类HiServiceImpl
package com.ws; public interface HiService { public void sayHi(); }
package com.ws; public class HiServiceImpl implements HiService { public void sayHi(){ System.out.println("Hello world"); } }
5 配置service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>HiService</name> <namespace> http://com.ws/HiService </namespace> <serviceClass> com.ws.HiService </serviceClass> <implementationClass> com.ws.HiServiceImpl </implementationClass> </service> </beans>
6 发布查看服务
以Web工程形式发布即可。
查看服务http://localhost:8080/Hi/services