先講一下我是如何一步一步加載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.csdn.net/detail/u011154682/5801665
待續。。。。