先讲一下我是如何一步一步加载cxf-rs的包的,过程比较坎坷啊。
- 首先,下载最新的karaf,apache-karaf-2.3.2,解包后,运行,在console输入features:addurl http://repo1.maven.org/maven2/org/apache/cxf/karaf/apache-cxf/2.7.5/apache-cxf-2.7.5-features.xml,然后输入features:list,
karaf@root> features:list
State Version Name Repository Description
[installed ] [2.7.5 ] cxf-specs cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-jaxb cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-abdera cxf-2.7.5
[uninstalled] [1.6.10 ] wss4j cxf-2.7.5
[installed ] [2.7.5 ] cxf-core cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-ws-policy cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-ws-addr cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-ws-rm cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-ws-mex cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-ws-security cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rt-security cxf-2.7.5
[installed ] [2.7.5 ] cxf-http cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-http-jetty cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-http-async cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-bindings-soap cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-jaxws cxf-2.7.5
[installed ] [2.7.5 ] cxf-jaxrs cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rs-security-xml cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rs-security-sso-saml cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rs-security-cors cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rs-security-oauth cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-rs-security-oauth2 cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-databinding-aegis cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-databinding-jibx cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-databinding-jaxb cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-databinding-xmlbeans cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-features-clustering cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-bindings-corba cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-bindings-coloc cxf-2.7.5
[uninstalled] [2.7.5 ] cxf-bindings-object cxf-2.7.5
可以看到很多feature,我这里只想安装cxf对rest的支持,所以console下输入features:install cxf-jaxrs。安装成功后,打开浏览器输入http:localhost:8181/cxf,会看到提示信息,如果不成功,则报404错误。
2.引入aries blueprint
aries的blueprint在我的开发环境一直有问题,我现在对比karaf,把相关的jar包全部引入了,下面是pom文件:
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.cm</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.proxy</groupId>
<artifactId>org.apache.aries.proxy.impl</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.aries</groupId>
<artifactId>org.apache.aries.util</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.proxy</groupId>
<artifactId>org.apache.aries.proxy.api</artifactId>
<version>1.0.0</version>
</dependency>
3.引入cxf-jaxrs的依赖包
<dependency>
<groupId>org.apache.servicemix.specs</groupId>
<artifactId>org.apache.servicemix.specs.jsr339-api-m10</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.3</version>
</dependency>
<!--cxf config -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-xml</artifactId>
<version>2.7.5</version>
<exclusions>
<exclusion>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-search</artifactId>
<version>2.7.5</version>
</dependency>
<!--<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.7.5</version>
</dependency>-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-management</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-activation_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.wsdl4j</artifactId>
<version>1.6.2_6</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.configadmin</artifactId>
<version>1.6.0</version>
</dependency>
有几个问题需要注意一下:
1.查看cxf-rt-transports-http包里面,发现有一个blueprint的注册文件,我们所访问的http://localhost:8181/cxf这个地址的servlet,就是通过这个xml文件注册的,这个包依赖于felix的configadmin ,我当时一直没有弄好,就是因为这个bundle没有加入。
2.cxf-rt-transports-http-jetty 这个包我注释掉,暂时还没什么影响。
下面我参考http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/做一个rest的例子,很简单的例子,一个接口,一个实现。此时不要按照例子讲的那样编码去注册,而是建立一个blueprint文件,由aries帮你自动完成注册。
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd">
<jaxrs:server id="customerService" address="/crm">
<jaxrs:serviceBeans>
<ref component-id="customerSvc"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="customerSvc" class="com.sunlf.tp.restfule.impl.MyServiceImpl"/>
</blueprint>
好了,工作完成了,打包,发布,然后启动容器,访问 http://localhost:8080/cxf :显示下图:
此时,说明你的rest服务已经注册好了,这个时候再输入http://localhost:8080/cxf/crm/myService/sayHello/world,显示:
最后需要说明的是,通过这种方式,我可以在其它的bundle里面声明rest服务,然后加入blueprint文件,就可以自动完成rest服务的发布了。根本不需要建web工程,加入CxfServlet,以及对spring的依赖,哈哈,是不是很方便啊。
另外,对ws的支持,应该也是同理。
把代码附上,可供参考。http://download.youkuaiyun.com/detail/u011154682/5801665
待续。。。。