Spring3整合cxf2.7.10

本文介绍了如何将Spring3与CXF2.7.10进行整合以创建Web服务。包括所需的依赖包、配置文件设置、接口定义与实现等关键步骤,并提供了完整的示例代码。

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

初学CXF,参考官方的文档对Spring3和CXF2.7.10做了一个整合,记录下来以备忘。


一、Spring用到的到包:


二、CXF用到的包:


三、工程示例


四、Web.xml文档

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0"
	metadata-complete="true">
	 	
	 <!-- webservice -->  
    <servlet>  
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/CXFService/*</url-pattern>  
    </servlet-mapping> 
		
	<!-- Spring -->	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml </param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>


五、applicationContext.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"
	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">

    <!-- =========================  CXF配置   ================================== -->

	<!-- Import Apache CXF Bean Definition 固定配置 -->
	<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" />

	<!-- 要发布成webservice的bean -->
	<jaxws:endpoint id="HelloWorld" address="/HelloWorld"
		implementor="com.lee.server.impl.ServiceHelloImpl" />


</beans>


六、WebService接口示例

package com.lee.server;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.lee.model.Cat;
import com.lee.model.User;
import com.lee.tools.FkXmlAdapter;

@WebService
public interface ServiceHello {
	
	public String say(String name);
	
	public List<Cat> getCatByUser(User user);
	
	/**
	 * CXF无法处理Map<String,Cat>,于是用FkXmlAdapter.calss处理
	 * @return
	 */
	@XmlJavaTypeAdapter(FkXmlAdapter.class)
	public Map<String,Cat> getAllCat();
	public String hehe();
}



七、接口实现类示例

package com.lee.server.impl;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;


import com.lee.model.Cat;
import com.lee.model.User;
import com.lee.server.ServiceHello;
import com.lee.server.UserService;

@WebService(endpointInterface="com.lee.server.ServiceHello")
public class ServiceHelloImpl implements ServiceHello{

	@Override
	public String say(String name) {
		// TODO Auto-generated method stub
		return name+"你好!";
	}

	@Override
	public List<Cat> getCatByUser(User user) {
		// TODO Auto-generated method stub
		UserService us = new UserServiceImpl();
		return us.getCatByUser(user);
	}

	@Override
	public Map<String, Cat> getAllCat() {
		// TODO Auto-generated method stub
		UserService us = new UserServiceImpl();
		return us.getAllCat();
	}
	
	@Override
	public String hehe(){
		return "hhe";
	}
	
	
}

八、在Tomcat中运行,浏览器中输入:http://127.0.0.1/WS_EE/CXFService/HelloWorld?wsdl 



PS: 在URL中WS_EE为工程名,CXFService为Web.xml中配置的路径,HelloWorld为applicationContext.xml中的address="/HelloWorld"。

示例源码下载地址:http://download.youkuaiyun.com/detail/libinsbox/6949443

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值