Apaache CXF 2.7 与Spring 3.0.7 集成分析

不点 阅读:218 2021-03-31 21:52:00 评论:0


下面通过实例演示Apache CXF 2.7与Spring3.0.7集成,发布和调用Web Service

开发环境:

win10 64位

tomcat7 64位

JDK6 64位

Apache CXF 2.7

Spring3.0.7


项目结构如下:


几个类的代码

package com.ws.cxf.dao; 
 
 
import javax.jws.WebMethod; 
import javax.jws.WebParam; 
import javax.jws.WebService; 
 
@WebService 
public interface IHelloWorld { 
	@WebMethod 
	String sayHello(@WebParam(name="username") String username); 
}

package com.ws.cxf.daoImpl; 
 
 
import javax.jws.WebService; 
import com.ws.cxf.dao.IHelloWorld; 
 
 
@WebService(endpointInterface="com.ws.cxf.dao.IHelloWorld",serviceName="helloWorld",targetNamespace="http://dao.cxf.ws.com/") 
public class HelloWorldImpl implements IHelloWorld{ 
 
	public String sayHello(String username) { 
		System.out.println("sayHello() is called"); 
		return username +" helloWorld"; 
	} 
}

相应的配置文件

<?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:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-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"> 
 
	<!--CXF配置 --> 
	<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  
		在spring中使用jaxws:endpoint元素来暴露Web Service  
		id:指在spring配置的bean的ID  
		Implementor:指明具体的实现类 
		Address:指明这个web service的相对地址 --> 
	<jaxws:endpoint id="helloWorld" implementor="com.ws.cxf.daoImpl.HelloWorldImpl" 
		address="/HelloWorld" /> 
</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"  
	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_2_5.xsd"> 
  <display-name>ApacheCxf</display-name> 
  <!-- 设置Spring容器加载配置文件路径 --> 
	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext-server.xml</param-value>  
    </context-param>  
     
 <!-- 加载Spring容器配置 --> 
    <listener>  
        <listener-class>  
         org.springframework.web.context.ContextLoaderListener 
        </listener-class>  
    </listener>  
     
<!-- 配置cxf的核心控制器 -->       
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>  
            org.apache.cxf.transport.servlet.CXFServlet   
        </servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
   <!-- 所有来自/webservice/*的请求交给cxf处理 -->  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/webservice/*</url-pattern>     
    </servlet-mapping>	 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

配置完成后,部署到tomcat,成功启动后。


使用SoapUI 测试webservice 


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号