CXF实现和Spring整合发布WebService

本文详细介绍了如何使用 Spring 和 CXF 实现 WebService 的发布过程,包括创建 Web 工程、配置 Web.xml、编写接口类、配置映射文件以及启动服务器进行发布部署等关键步骤。

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

这些天项目框架升级..........................必须学习CXF进行发布WebService.............................那么必不可少的就是和Spring整合


1..................................首先创建Web工程SpringCXF


2.................................导入必须的架包(本人尝试了一下需找最低配置,至少需要的架包如下,你可以直接把apache-cxf-2.6.1下lib包直接copy到项目lib目录下,一定OK,呵呵)


3............................下一步要进行Web.xml的配置,来通过servlet进行Spring的过滤,从而能够查找到发布的接口

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!--spring需要加载的配置文件-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext-server.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--
	    cxf服务启动servlet
	-->
	<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>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

4.....................................在src目录下新建applicationContext-server.xml文件,用来进行Spring映射,并添加与CXF整合配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	    			 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
	       				http://www.springframework.org/schema/context   
 			http://www.springframework.org/schema/context/spring-context-3.0.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" />
</beans>

5..................................编写接口类并添加CXF注解

package com;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;


@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {

	public String getUserByName(@WebParam(name = "username") String username);
	public void setUser(String username);
}

6..................................编写接口实现类同时加上CXF注解

package com;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {

	public String getUserByName(@WebParam(name ="username")String username) {
		return "Hello:"+username;
	}

	public void setUser(String username) {
		System.out.println(username);
	}

}

7.................................配置Spring映射文件,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	    			 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
	       				http://www.springframework.org/schema/context   
 			http://www.springframework.org/schema/context/spring-context-3.0.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" />

	<bean id="userServiceBean" class="com.ComplexUserService" />
	
	<jaxws:server id="userService" serviceClass="com.IComplexUserService"
		address="/userService">
		<jaxws:serviceBean>
			<ref bean="userServiceBean" />
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

8............................启动服务器进行发布部署,在浏览器中输入http://localhost:8080/SpringCXF/services/userService?wsdl,显示如下结果


9...........................发布成功。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值