maven+CXF+Spring+tomcat 開發webservice

崑崙子發表於2015-02-06

以前做專案時做專案時有做過這塊內容,今天因為與第三方ERP系統對接需要要在系統中加上webservice的服務端,與呼叫客戶端,中間出了好幾處錯,現整理下備忘;

先看下我專案的目錄結構:


我們的專案使用的是spring+mybatis 採用maven管理;在這基礎上加webservice的大致步驟:

1.pom檔案新增cxf所需包依賴

 <!--CXF配置 data:2015年02月05日 18:04********************************************************-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-common</artifactId>
            <version>2.5.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!--CXF配置 data:2015年02月05日 18:04********************************************************-->

2.web.xml中加入cxf支援

<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/com/ningpai/web/config/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!--CXF配置 2015-02-06 10:12******************************************-->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<!--CXF配置 2015-02-06 10:12 ******************************************-->

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

	<!--CXF配置 2015-02-06 10:12*******************************************-->
		<servlet-mapping>
			<servlet-name>CXFServlet</servlet-name>
			<url-pattern>/ws/*</url-pattern>
		</servlet-mapping>
	<!--CXF配置 2015-02-06 10:12 *******************************************-->

3.在業務模組中編寫spring支援xml將webservice服務類用spring進行管理

<?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:context="http://www.springframework.org/schema/context"
	   xmlns:jaxws="http://cxf.apache.org/jaxws"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:task="http://www.springframework.org/schema/task"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
		   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		   http://www.springframework.org/schema/context
		   http://www.springframework.org/schema/context/spring-context-2.5.xsd
		   http://www.springframework.org/schema/aop
		   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		   http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		   http://www.springframework.org/schema/task
		   http://www.springframework.org/schema/task/spring-task-3.0.xsd
		   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Memcached配置 <import resource="classpath:simplesm-context.xml" /> <aop:aspectj-autoproxy 
		/> <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"> 
		<property name="cacheClientFactory"> <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" 
		/> </property> <property name="addressProvider"> <bean class="com.google.code.ssm.config.DefaultAddressProvider"> 
		<property name="address" value="192.168.2.99:11211" /> </bean> </property> 
		<property name="configuration"> <bean class="com.google.code.ssm.providers.CacheConfiguration"> 
		<property name="consistentHashing" value="true" /> </bean> </property> </bean> -->

	<!--CXF配置 2015年02月06日 11:09>>>>>>>>>>>>>>>>>-->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<!--CXF配置 2015年02月06日 11:09<<<<<<<<<<<<<<<<<<-->

	<!--CXF Service配置 2015年02月06日 11:27-->
	<jaxws:endpoint id="productService" address="/productService" implementorClass="com.kunge.ws.server.IProductService">
		<jaxws:implementor>
			<bean id="productServiceImpl" class="com.<span style="font-family: Arial, Helvetica, sans-serif;">kunge</span><span style="font-family: Arial, Helvetica, sans-serif;">.ws.server.impl.ProductServiceImpl"></span>
				<property name="goodsMapper" ref="GoodsMapper"/>
			</bean>
		</jaxws:implementor>
		<jaxws:features>
			<bean class="org.apache.cxf.feature.LoggingFeature" />
		</jaxws:features>
	</jaxws:endpoint>

</beans>


這裡一定要注意<bean xmlns中對cxf支援的兩處引用,沒有會報錯,即匹配很全面,但未找到jaxws定義之類的錯;
xmlns:jaxws="http://cxf.apache.org/jaxws"    <pre name="code" class="html">http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd



4.還是web.xml中新增對於自定義xml的引用

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/com/kunge/web/config/ApplicationContext.xml,
<span style="white-space:pre">			</span>     classpath*:com/kunge/web/config/ws-servlet.xml
<span style="white-space:pre">		</span></param-value>
	</context-param>

這裡路徑要寫對,否則會找不到webservice所寫的webservice類,即cxf為定義之類的錯誤;


這裡只有降到相關的配置,具體程式碼網上有很多實現;本來是想用純註解的方式寫的,因為我dao層service層,controller都是註解,但是試了半天總是顯示no  services。。還好在cxf的配置中可以引用註解初始化的類;即

<bean id="productServiceImpl" class="com.kunge.ws.server.impl.ProductServiceImpl">
		<property name="goodsMapper" ref="GoodsMapper"/>
</bean>


這裡的goodsMapper其實是使用@Repostory註解來管理的;當然後續還會繼續研究這塊怎樣用純註解來實現;



相關文章