天天看點

【webservice】CXF結合spring釋出簡單的webservice服務

本文先釋出在:封宸落宇    再同步釋出到本部落格!

CXF結合spring釋出簡單的webservice服務!

【webservice】CXF結合spring釋出簡單的webservice服務

基于CXF 釋出webservice服務

1、建立web工程 ,添加spring(3.1.3)包,添加cxf(2.2.6)依賴包

2、建立配置檔案:applicationContext-webservice.xml

檔案内容:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <? 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"      xsi : schemaLocation = "http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-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" / >            < ! -- 定義實作類 implementor參數寫的是 spring bean的名稱,和釋出位址 -- >      < jaxws : endpoint id = "SyncAddressBookService" implementor = "#SyncAddressBookService" address = "/SyncAddressBookService" publish = "true" >          < jaxws : features >                     < bean class = "org.apache.cxf.feature.LoggingFeature" / >                  < / jaxws : features >          < / jaxws : endpoint >          < bean id = "SyncAddressBookService"   class = "com.zte.ucm.sync_in_soap.sync_ipop_service.impl.SyncAddressBookServiceImpl" / > < / beans >

 然後在applicationContext.xml中将applicationContext-webservice.xml添加進來。

1 < import resource = "applicationContext-webservice.xml" / >

3、在web.xml中,添加cxf servlet定義!

1 2 3 4 5 6 7 8 9 10 < servlet >          < servlet - name > UserPrdBindService < / servlet - name >          < servlet - class > org . apache . cxf . transport . servlet . CXFServlet < / servlet - class >          < load - on - startup > 1 < / load - on - startup >          < / servlet >            < servlet - mapping >          < servlet - name > UserPrdBindService < / servlet - name >          < url - pattern > / UserPrdBindService / * < / url - pattern >          < / servlet - mapping >

上面定義的cxfservlet對于URL中帶有UserPrdBindService的請求都将過濾,對于上面的定義通路:http://ip/UserPrdBindService/SyncAddressBookService?wsdl就能夠通路到釋出的服務。

4、建立服務接口

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @WebService ( targetNamespace = "http://www.citen.com/SyncAddressBookService/" , name = "SyncAddressBookService" ) public interface SyncAddressBookService {             public String inputAddressBook ( String input ) ;           public String inputAddressBook ( @WebParam ( name = "user" ) User input ) ;           public @WebResult ( partName = "user" ) User inputAddressBook ( String input ) ; }

 /

5、建立接口實作類

1 2 3 4 5 6 7 8 9 10 @WebService ( targetNamespace = "http://www.citen.com/SyncAddressBookService/" , endpointInterface = "com.zte.ucm.sync_in_soap.sync_ipop_service.SyncAddressBookService" ) public class SyncAddressBookServiceImpl implements SyncAddressBookService {      private static final Logger log = Logger              . getLogger ( SyncAddressBookServiceImpl . class ) ;        public String inputAddressBook ( String xml ) {          return "0" ;      }   }

6、部署并啟動,通路:http://ip/UserPrdBindService/SyncAddressBookService?wsdl  能夠擷取到wsdl檔案,恭喜你已經成功利用cxf結合spring釋出了一個webservice服務了!

服務釋出了,那怎麼通路呢,請檢視我的另一篇部落格!!

繼續閱讀