使用CXF+spring创建一个web的接口项目

一、web project整合spring

1.1、打开Myeclipse,建立web project(eclipse为dynamic web project),使用J2EE5.0。

1.2、加入Srping的基本jar包(无需事务等)

org.springframework.beans-3.1.1.RELEASE.jar

commons-logging.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
javassist-3.11.0.GA.jar

1.3、新建源文件夹(source folder)conf,位于项目下,加入applicationContext.xml到该文件夹,内容例如以下:

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

	
</beans>

1.4、在web.xml中web-app节点下加入监听,例如以下:

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

执行项目,正常执行则说明正常。

二、开发webservice服务

新建RegeditService类,例如以下:

package zxn.ws.service;

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

@WebService
public interface RegeditService {
	/**
	 * 注冊方法
	 * @param username
	 * @param password
	 * @return
	 */
	public String regedit(@WebParam(name = "username")String username, @WebParam(name="password")String password);
}
新建RegeditServiceImpl类,例如以下:
package zxn.ws.service;

import javax.jws.WebService;

@WebService(endpointInterface="zxn.ws.service.RegeditService",serviceName="Regedit", targetNamespace="http://service.ws.zxn/")
public class RegeditServiceImpl implements RegeditService {
	/**
	 * 注冊方法
	 * @param username
	 * @param password
	 * @return
	 */
	public String regedit(String username, String password) {
		return username+",你已成功注冊!";
	}
}

注意:targetNamespace中的包名倒着写,最后要加"/",否则报错。

三、spring整合cxf

3.1、加入jar包

cxf-2.7.8.jar
neethi-3.0.2.jar
xmlschema-core-2.0.3.jar
wsdl4j-1.6.3.jar
asm-3.3.jar
3.2、applicationContext.xml中加入例如以下内容:

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- webservice配置 ,myeclipse可能检測到此处有错没影响-->
    <jaxws:endpoint id="regeditService" implementor="zxn.ws.service.RegeditServiceImpl" address="/Regedit" />
3.3、在web.xml中加入例如以下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>

     <servlet-mapping>
         <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
     </servlet-mapping>

部署到tomcat,訪问地址:http://localhost:8080/CXFWS/services(最后的services是3.3中配置的訪问路径),例如以下图则表示成功:

wsdl文档例如以下图:


另附上源码地址:http://download.csdn.net/detail/zxnlmj/7457693

原文地址:https://www.cnblogs.com/lcchuguo/p/4046064.html