cxf与spring的整合使用

本文指导如何创建一个包含Spring容器和CXF Web服务的项目,详细配置web.xml和applicationContext-server.xml文件,建立Webservice接口并解决配置文件问题。

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

先建一个web项目,CXF_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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <!-- 加载Spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
 
<servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
 
<servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

配置applicationContext-sercer.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.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.gg.service.ComplexUserService"/>
<jaxws:server id="userService" serviceClass="com.gg.service.IComplexUserService" address="/Users">
    <jaxws:serviceBean>
        <!-- 要暴露的 bean 的引用 -->
        <ref bean="userServiceBean"/>
    </jaxws:serviceBean>
</jaxws:server>
</beans>

建一个webservice接口

package com.gg.service;


import javax.jws.WebService;


@WebService
public class ComplexUserService implements IComplexUserService {




@Override
public String sayHello(String userName) {
System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");  
return "Hello,"+userName;
}


}

实现该接口

package com.gg.service;


import javax.jws.WebParam;
import javax.jws.WebService;




@WebService
public interface IComplexUserService {
public String sayHello(@WebParam(name="userName") String userName);
}

最后启动tomcat服务器。访问http://localhost:8080/CXF_Spring/Users?wsdl


遇到的问题:启动Tomcat一直说我的配置文件applicationContext-server.xml有问题。经过多次修改还是提示同个问题。

调试bug真的好他妈的痛苦,最终在项目classes文件夹中发现生成的applicationContext-server.xml文件的修改日期不变,原来这个文件一直没重新编译成功。

解决办法:clean一个该项目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值