本文先发布在:封宸落宇 再同步发布到本博客!
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服务了!
服务发布了,那怎么访问呢,请查看我的另一篇博客!!