最新版本CXF 2.7.3与spring整合demo

1、从cxf官方网站下载最新版本cxf2.7.3,创建web项目,准备好spring3.3.2的基本框架,包括spring MVC

2、web.xml添加配置(前提已经配置好spring MVC):

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/classes/spring/application-beans.xml,
   /WEB-INF/classes/spring/cxf-service.xml,
   /WEB-INF/classes/spring/spring-MVC.xml
  </param-value>
 </context-param>
  <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>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/services/*</url-pattern>
</servlet-mapping>  

 3、引入cxf的jar包(多余jar包不要,前提已经配置spring MVC时候已经引入好了其他依赖jar包,一下jar包来自下载的CXF2.7.3):

cxf-2.7.3.jar
httpasyncclient-4.0-beta3.jar
httpclient-4.2.1.jar
httpcore-4.2.2.jar
httpcore-nio-4.2.2.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.3.jar

 4、CxfService.java接口类:

import javax.jws.WebService; 

@WebService 
public interface  testWebService{
     public String taskService(String strXml) throws Exception;
}

 5、CxfService.java的实现类CxfServiceImpl.java:

import javax.jws.WebService;

@WebService(endpointInterface = "CxfService")
public class CxfServiceImpl implements CxfService{
             public String testWebService(String strXml) throws Exception {
		System.out.println("-------HELLO WEBSERVICE---------");
	                   return null;
	}			 
}

6、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"
	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 -->
<!-- 暴露接口,implementor表示要 暴露的bean,address表示wsdl的访问路径-->
	<jaxws:endpoint id="cxfServiceJaxws" implementor="CxfServiceImpl" address="CxfService" />
</beans>

 7、部署项目,启动服务器,浏览器输入:http://127.0.0.1:8080/APPNAME/services/CxfService?wsdl即可看到发布的webservice。

猜你喜欢

转载自hanlin123.iteye.com/blog/1838531