Spring+Apache CXF集成

本文详细介绍如何将Spring 4.2.5与Apache CXF 3.1.5集成,通过Maven搭建环境,并配置Webservice文件及web.xml。此外,还提供了WebService接口和实现类的具体代码。

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

Spring+Apache CXF集成

   本文主要介绍Spring4.2.5与Apache CXF3.1.5集成。采用Maven搭建环境。

   1 配置pom.xml文件,如下所示:

 

 

    <properties>
        <cxf.version>3.1.5</cxf.version>
        <spring.version>4.2.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>  

 

2 配置Webservice 文件spring-cxf-service.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd 
    	http://www.springframework.org/schema/context 
	 	http://www.springframework.org/schema/context/spring-context-4.2.xsd  
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd
		http://cxf.apache.org/transports/http/configuration    
        http://cxf.apache.org/schemas/configuration/http-conf.xsd">


	<jaxws:endpoint id="iWebService" implementor="stu.sis.IWebServiceImpl"
		address="/iWebService">
	</jaxws:endpoint>
</beans>  


3 配置web.xml

<!-- 加载酨文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-cxf-service.xml</param-value>
	</context-param>
	<!-- 注册Spring监听 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

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


4 编写WebService接口
  IWebService接口:

package stu.sis;
import javax.jws.WebService;

@WebService
public interface IWebService {
	public String operation(String username);  
}


IWebServiceImpl 实现类:

package stu.sis;

import javax.jws.WebService;

@WebService
public class IWebServiceImpl implements IWebService {
	@Override
	public String operation(String username) {

		return "myWebservice " + username;
	}
}


5 发布服务,在浏览器输入 http://127.0.0.1:8080/ws/webservice , 测试是否成功。
  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值