Apache CXF开发WebService

本文介绍如何使用Apache CXF框架结合Spring进行WebService开发。主要包括创建项目、编写Service接口及其实现类、配置Web.xml等内容,并展示了如何通过Spring管理WebService。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apache CXF是一种新型的WebService框架,使用它开发WebService将会极大的提高开发效率。CXF与Spring的集成是非常自然的,如果你不知道Spring请问Google。
CXF项目主页:http://cxf.apache.org
由于CXF集成了很多主流的工具包,所以它的体积非常大,30M+,有兴趣的研究下哪些包是非必须的,烦请告知。
费话少说,开工。
一、在Eclipse中建立一个Dynamic Web project,添加CXF/lib下所有jar到项目的lib中
二、编写Service类
2.1先建立一个接口

package com.iflysse.cxf;
import javax.jws.WebService;
@WebService
public interface IVote {
public boolean vote(String username, int point);
public int getVoteUserTotal();
public int getVotePointTotal();
}

2.2建立Service类,实现接口方法

package com.iflysse.cxf;
import javax.jws.WebService;
@WebService
public class Vote implements IVote {
private static int pointTotal;
private static int userTotal;
public int getVotePointTotal() {
return pointTotal;
}
public int getVoteUserTotal() {
return userTotal;
}
public boolean vote(String username, int point) {
userTotal++;
pointTotal+=point;
return true;
}
}

三、在Web.xml中配置CXF,使其生效
3.1在Web.xml中添加CXFServlet,为用户提供访问入口

<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

3.2由于CXF与Spring是天然集成的,所以在Web.xml中添加Spring的配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.3在WEB-INF下建立beans.xml内容如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="vote" implementor="com.iflysse.cxf.Vote"
address="/Vote" />
</beans>


四、运行项目,检验成果,访问http://localhost:8080/CXFDemo/services/Vote?wsdl

注:CXF与Spring集成的意义
松耦合,通过配置实现WebService的发布
可以通过Spring容器对WebService管理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值