September 1, 2013 · osgi camel petprojects

Building CXF REST Service in OSGi for Karaf

I'll leave it to the experts to tell how awesome OSGi is. Among the many benefits, I could tell you why we picked up OSGi for a pet project - Modularity, avoiding JAR hell and dynamic updates (hey, why not?)

We chose Apache Felix (an OSGi framework specification implementation) and Apache Karaf (ummm, how do I put this - something like an app server for OSGi applications). Besides serving as an OSGi container, Karaf has a lot of awesome features (pun intended). And we like the idea of managing multiple Karaf instances managed through Zookeeper.

Enough talk, let's see some code. This is a rudimentary tutorial on how to run a basic OSGi CXF-JAX Rest Service on Karaf. Given a REST parameter (a name), the service would just return Hello, (name). That's it !! The entire project could be downloaded here

So, if the request is http://localhost:8181/cxf/karafsimple/say/hello/arun, the response would be Hello, arun

Please note that this project does not have any domain models and therefore has only two projects - one for the REST (Controller) and the other for the actual service implementation.

The project structure looks like this :

Project structure

Step 1 - Service Implementation

This project just has two 'useful' things - the HelloService interface and the HelloServiceImpl class.

package me.rerun.karafcxf.service.impl;

public interface HelloService {

    public String sayHello(String name);

package me.rerun.karafcxf.service.impl;

public class HelloServiceImpl implements HelloService {

    public String sayHello(String name) {
        return "Hello, "+name;


Stupid right?

Step 2 - REST project

Similar to the Service project, this project also has just two notable things - the HelloRestService interface and the HelloRestServiceImpl class.




//Maps for the `say` in the URL

public interface HelloRestService {

    @Path("hello/{name}") //Maps for the `hello/John` in the URL
    public String handleGet(@PathParam("name") String name);



The HelloRestServiceImpl does nothing but calls the HelloService which gets injected through Blueprint Dependency Injection. Hey, does the inject look very familiar to Spring DI? Exactly !! The Blueprint DI is heavily influenced by Spring DI. In fact, the original work for blueprint is done by Spring.


import me.rerun.karafcxf.service.impl.HelloService;

public class HelloRestServiceImpl implements HelloRestService{

    //Just like Spring.  Please add Getters/Setters. Blueprint annotations are still work in progress
    private HelloService helloService;

    public String handleGet(String name){

        return helloService.sayHello(name);



    public HelloRestServiceImpl(){

        Getters and Setters

    public HelloService getHelloService() {
        return helloService;

    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;


Step 3 - Injections

XMLs (of any name) inside OSGI-INF/blueprint folder will get picked up for DI scanning.


Does two things in one tag :

  1. Registers the HelloService into the service registry for lookup
  2. Says that its implementation is HelloServiceImpl

<blueprint xmlns=""

    <service ref="helloServiceBean" interface="me.rerun.karafcxf.service.impl.HelloService">
        <bean class="me.rerun.karafcxf.service.impl.HelloServiceImpl"/>


In fact, you could do this in two separate steps. More on that here.

<service id="helloServiceBean" ref="helloServiceImpl"
    interface="me.rerun.karafcxf.service.impl.HelloService" />

<bean id="helloServiceImpl" class="me.rerun.karafcxf.service.impl.HelloServiceImpl" />


<blueprint xmlns=""
<!-- 1 -->
    <cxf:bus id="cxfBus1">

<!-- 2 -->
    <jaxrs:server address="/karafsimple" id="someRestService">
            <ref component-id="restServiceImpl"/>

<!-- 3 -->
    <!-- Implementation of the rest service -->
    <bean id="restServiceImpl" class="">
        <property name="helloService" ref="helloServiceBean"/>  <!--Points to the reference below -->

<!-- 4 -->
    <!-- This has to point to the service registered through serviceimpl.xml in the service.impl project -->

    <reference id="helloServiceBean"


  1. cxf-bus is the bus configuration for CXF. It is like the manager for all CXF services. The most common use as far as I know is to configure custom interceptors (for auditing, request/response manipulation, headers manipulation etc)

  2. the jaxrs:server initiates a server which would start listening to the URLs that we mapped for. Of course, we would want to map the handlers for the URLs and those go under the serviceBeans.

  3. The third note in the XML is the just the restServiceImpl configuration and the injection of the helloService as a property inside the HelloRestServiceImpl

  4. The reference tag will lookup the service registry for a bean with the same id.

Step 4 - KAR - Karaf Archive (Optional but easier this way)

Technically, you could just start throwing the bundles generated through the Maven into to the deploy directory of karaf. However, as the project goes big and your dependencies are becoming many, it is advisable to use the .kar archive. Picture .kar as your .war or .ear bundle. A .kar archive, internally, looks something like this :

.kar internals

Building .kar

Building the .kar is composed of two steps :

Step 1


Similar to your web descriptor or application descriptor, we have the features descriptor in Karaf to represent the entire repository that we are bundling together to compose our application.

In our case, we don't have any external .jar dependencies except for the cxf and the http service for which we are using the in-built features available inside karaf.

<?xml version="1.0" encoding="UTF-8"?>
<features xmlns=""
    <feature name="karafcxf" description="karaf cxf hello" version="1.0-SNAPSHOT" resolver="(obr)">


    <feature name="this-project-dependants">
        <!-- Dependent bundles if you have any. This project doesn't have one


Step 2

Maven plugin configuration to create .kar, which indicates where your feature.xml is located (Note that this file is located inside your karaf project)



Notice the wrap protocol in front of the mvn protocol in few bundles as in


Not all Jars are OSGi ready but they would obviously be used as a dependency in our project. In those cases, Karaf notices the wrap protocol and bundles the JAR into an OSGi bundle. In case of doubt, just drop off the wrap and Karaf would complain that the jar is not OSGi compatible. (Alternatively, you could open up each of the dependant jars and check their manifest files)

013-08-28 01:38:48,669 | WARN  | raf-2.3.2/deploy | KarArtifactInstaller             | eployer.kar.KarArtifactInstaller  192 | 24 - org.apache.karaf.deployer.kar - 2.3.2 | Unable to install Kar feature xx-xxx-xxxxxx/0.0.0
org.osgi.framework.BundleException: Jar is not a bundle, no Bundle-SymbolicName mvn:org.apache.httpcomponents/httpcore/4.2.4
	at org.apache.karaf.features.internal.FeaturesServiceImpl.installBundleIfNeeded([26:org.apache.karaf.features.core:2.3.2]
	at org.apache.karaf.features.internal.FeaturesServiceImpl.doInstallFeature([26:org.apache.karaf.features.core:2.3.2]
	at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeatures([26:org.apache.karaf.features.core:2.3.2]
	at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeature([26:org.apache.karaf.features.core:2.3.2]
	at Proxy508d2419_d21e_4a93_b7fb_26e28d2f03a6.installFeature(Unknown Source)[:]
	at org.apache.karaf.deployer.kar.KarArtifactInstaller.installFeatures([24:org.apache.karaf.deployer.kar:2.3.2]
	at org.apache.karaf.deployer.kar.KarArtifactInstaller.install([24:org.apache.karaf.deployer.kar:2.3.2]
	at org.apache.karaf.deployer.kar.KarArtifactInstaller.update([24:org.apache.karaf.deployer.kar:2.3.2]
	at org.apache.felix.fileinstall.internal.DirectoryWatcher.update([6:org.apache.felix.fileinstall:3.2.6]
	at org.apache.felix.fileinstall.internal.DirectoryWatcher.update([6:org.apache.felix.fileinstall:3.2.6]
	at org.apache.felix.fileinstall.internal.DirectoryWatcher.process([6:org.apache.felix.fileinstall:3.2.6]

Changing Log Levels

For all our development environment, we would want to increase our log level to get more feedback from Karaf. This could be achieved by modifying the org.ops4j.pax.logging.cfg file located in your <karaf-installation-directory>/etc

Changing Karaf Log level

Step 5 - Other Maven configuration

Not technically a step because we would have covered this from the beginning anyway. And nothing fancy here.

Parent pom.xml

  1. Has the rest of the sub-modules configured.
  2. The various library dependencies configured

<project xmlns=""






Rest and ServiceImpl pom.xml

The other poms.xmls aren't interesting

They just affiliate themselves to the parent pom with the parent tag as in


Step 6 - Bring up Karaf

My Karaf installation is located at : /Users/Gabriel/apps/apache-karaf-2.3.2

**Start Karaf : **


Installing CXF and HTTP services

features:chooseurl cxf 2.7.5

features:install http cxf

Checking whether your bundle is installed and your service running



Karaf verification commands

Stop Karaf

Ctrl +D (Please note that Ctrl +C closes the connection abruptly instead of stopping Karaf)

In case of accidental Ctrl+C and if Karaf isn't starting properly, do a

rm -rf <installation-dir>/data/cache/*

URL Mapping

Like I mentioned earlier, the target request URL will be something like http://localhost:8181/cxf/karafsimple/say/hello/arun and the response would be Hello, arun

  1. The HelloRestService interface has all the JAX RS annotations for the URL mapping. Well, technically, the interfaces just maps for say/hello/(name)

  2. The karafsimple in the URL is derived from the the JAX RS address in blueprint.xml (about that later in Step 3 - rest.xml).

  3. The cxf is a default if you deploy a CXF service on Karaf which obviously you could change.