Monday, March 31, 2008

Using Jersey (JSR311) inside OSGi with Spring-DM

Or: using REST & OSGi to create a number guessing game

Jersey is an implementation of JSR 311 also know as JAX-RS. It makes writing a REST service quite simple with Java Annotations. So I wanted to use Jersey inside my OSGi bundle with the OSGi HTTP Service. However, as I found out, Jersey is not written to run in an OSGi container...

Fortunately, the problematic code in Jersey is pluggable. In this posting I'm writing a little Jersey REST Application that runs in my OSGi container. I'm using Spring-DM (aka Spring-OSGi) to do the OSGi Service dependency injection which keeps my code really lean & clean (Spring Clean ;) .

To illustrate I wrote a little number guessing game. The very first program I ever wrote was a game like this. This was on the Commodore Pet computer and I was about 10 years old. So beware...

Development Environment Setup
The Jersey version I got is 0.7-ea, which I downloaded from the Jersey download site.

I'm using the Equinox OSGi runtime in Eclipse 3.3.2 (get it here) with Spring-DM added in.

I downloaded Spring-DM 1.0.2 from here and copied all the jars from the dist and the lib directory of the download in my eclipse/plugins directory. The jars in the Spring-DM lib directory have been OSGi-ified by the Spring guys so we can nicely drop them in as OSGi bundles.

Bundle Dependencies
The general dependency picture is like the image below. I'm writing a Jersey app in a bundle that depends on a library bundle which contains the Jersey jars. This way multiple bundles can use the Jersey functionality to implement REST services while sharing the same Jersey Library bundle.

The Jersey Library Bundle
This is a simple wrapper around the Jersey libraries. At the moment I'm only depending on jersey.jar, jsr311-api.jar and asm-3.1.jar. So I'm simply creating an OSGi Bundle Project in Eclipse (Plug-in Project - see this posting for some details on how to do this), add these three jars from the Jersey install to the root of the bundle and then put them on the Bundle-Classpath. Finally simply export all the classes defined by these bundles. I guess a subset of the classes would do too, anyone fancy figuring out what the minimum Export-Package would be?

My MANIFEST.MF looks like this:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JerseyLibBundle
Bundle-SymbolicName: JerseyLibBundle
Bundle-Version: 1.0.0
Bundle-ClassPath: asm-3.1.jar,
Import-Package: javax.servlet,
Export-Package: ...,
 ... export all the packages in these jars ...

That's the Jersey Library Bundle done. My Eclipse screen looks like this (click on it to see all the details):

The REST Bundle
This is the interesting bit. Here's where we'll have our REST Guessing Game implemented.

The REST application will be available on:

Lists all the numbers available in XML format.

Returns information about the requested number in text format. The information says what number it is (the same one you asked for) and whether or not you guessed the right number.

I have a class:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Number {
  enum Location {LESS, FOUND, MORE};
  private static final int MAX_NUM = 10;

  static List<Number> NUMBERS;
  static {
    // Initialize with numbers 1 to 10,
    // and pick a random on to guess.
    NUMBERS = new ArrayList<Number>();

    Random random =
      new Random(System.currentTimeMillis());
    int theOne = random.nextInt(MAX_NUM) + 1;

    for (int i=1; i <= MAX_NUM; i++) {
      Location location;
      if (i == theOne) {
        location = Location.FOUND;
      } else if (i < theOne) {
        location = Location.LESS;
      } else {
        location = Location.MORE;
      NUMBERS.add(new Number(i, location));

  private final int number;
  private final Location location;
  public Number(int num, Location l) {
    number = num;
    location = l;

  public int getNumber() {
    return number;

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("I'm number ");
    sb.append(". ");
    switch (location) {
    case FOUND:
      sb.append("You guessed right!");
    case LESS:
      sb.append("You need to guess higher.");
    case MORE:
      sb.append("You need to guess lower.");
    return sb.toString();

Next thing we need is a JSR311 compliant class that binds our Number class to a REST API:


public class NumberResource {
  public String listNumbers() {
    StringBuilder sb = new StringBuilder();

    sb.append("<!-- Guess the right number! The following numbers are available. -->");
    for (Number number : Number.NUMBERS) {
    return sb.toString();

  public String getNumber(@PathParam("id") String id) {
    try {
      int i = Integer.parseInt(id);
      Number n = Number.NUMBERS.get(i-1);
      return n.toString();
    } catch (Throwable th) {
      return "Sorry that number is out of range";

That's our REST implementation done. Only thing left to do is use the OSGi HTTPService to launch the Jersey Servlet and tell it what our REST Resource are, right? You wish! You would think that the following piece does the trick:

HttpService httpService = ...; // The OSGi HTTP Service
Hashtable initParams = new Hashtable();
  // The following are standard Jersey properties

Servlet jerseyServlet =
httpService.registerServlet("/fun", jerseyServlet, initParams, null);

But hey! I'm getting an ugly exception: The ResourceConfig instance does not contain any root resource classes.

This is because the class contains OSGi unfriendly classloader code (it tries to treat classes as resources, which at least does'nt work on Equinox). Luckily this class is plugged into Jersey, so I can write an OSGi-friendly version. PackagesResourceConfig searches the specified packages for any classes with the JSR311 annotations.
My alternative implementation is called jersey_osgi.common.OSGiResourceConfig. The source is slightly too big for the posting, so just click on the link if you want to see it.

For my OSGi version I have to actually specify the classes that are annotated in a property called jersey_osgi.classnames. So I change to properties like this:
HttpService httpService = ...; // The OSGi HTTP Service
Hashtable initParams = new Hashtable();

Servlet jerseyServlet =
httpService.registerServlet("/fun", jerseyServlet, initParams, null);

And run it. Now I'm getting: No WebApplication provider is present aaaargh! After some debugging I found out that Jersey heavily depends on the META-INF/services SPI architecture which is quite non-OSGi friendly. There are only two possible solutions to this that I can think of:

  1. Export-Package in the Jersey Library Bundle. This works but is kinda nasty because you can only do it once in a single OSGi runtime. If you have multiple bundles exporting the same package, the OSGi runtime will only link you to only one of them for this package. If multiple bundles export you will get unexpected behaviour.

  2. Copy the META-INF/services directory from the jersey.jar into our own bundle. Other projects with OSGi bundles seem to be taking this approach too.

So I'm taking approach 2 as well and copy all the META-INF/services files of jersey.jar into my bundle. And now it works! I can nicely build my Jersey apps as OSGi bundles that all depend on shared Jersey code.

Where's the Spring stuff?
Spring-DM makes it really easy for me to interact with the OSGi HTTP Service as it simply injects it in my JerseyServletFactory class which then registers the Jersey Servlet. To trigger this, I put a jersey_osgi_spring.xml file in the META-INF/spring directory of my bundle:

<beans xmlns=""

  <osgi:reference id="httpServiceRef"

  <bean name="jerseyServletFactory"
    class="jersey_osgi.common.JerseyServletFactory" >
    <property name="jerseyClassNames"
    <property name="rootContext" value="/fun"/>
    <property name="httpService" ref="httpServiceRef"/>

The JerseyServletFactory registers the Jersey Servlet with the OSGi HTTP Service. For this it needs that HTTP Service in the first place. Those who had the pleasure of using the pure OSGi API's to get hold of an OSGi Service know that this is actually slightly tricky. So OSGi Declarative Services come to the rescue (which I used in the earlier article about writing a Card Game with OSGi Services). Spring-DM provides an alternative to OSGi-DS and brings with it all of the goodies that Spring has to offer. With the above Spring file, my JerseyServletFactory is nicely reusable (the Root Context and Jersey resource classes are also externalized into the Spring Configuration file) and no bigger than this:
package jersey_osgi.common;

import java.util.Hashtable;
import javax.servlet.Servlet;
import org.osgi.service.http.HttpService;

public class JerseyServletFactory {
  private String classNames;
  private String rootContext;

  // Spring uses the setters to inject our stuff
  public void setJerseyClassNames(String names) {
    classNames = names;

  public void setRootContext(String ctx) {
    rootContext = ctx;

  public void setHttpService(HttpService httpService) throws Exception {
  Hashtable<String, String> initParams =
    new Hashtable<String, String>();

    Servlet jerseyServlet =
      new ServletContainer();
      jerseyServlet, initParams, null);

Running it all inside Eclipse
Running Spring-DM inside Equinox as shipped with Eclipse requires careful definition of the runtime environment. You set this up by going to Run | Open Run Dialog ... in Eclipse where you create a new OSGi Framework run configuration. Deselect the Target Platform tickbox and only select the following bundles:

Like this (I edited the unselected bundles out of the screenshot):

I found that if I don't do it exactly like this that I'm getting problems with the correct logger being instantiated, but this configuration works. Just hit 'Run' and you're off. You can now access the REST application from http://localhost/fun/numbers

I can guess a number: http://localhost/fun/numbers/5

... and try again: http://localhost/fun/numbers/3


Get the code
You can get all the code (Apache License) as usual from the google code site. It's at available as 2 Eclipse projects at The easiest way to get them into your Eclipse installation is by using an SVN client inside Eclipse and check them out as projects. This is described in more detail in this posting (just search for the 'subclipse' in there).


cmoulliard said...

Very interesting post David. I'm currently working on camel-web (using Jersey) that I would like to modify to be able to work on Apache Felix Karaf. I see in your post that you have used the command Bundle-classpath to import jersey classes.

Have you created the list of packages to be imported/exported in MANIFEST file ?

David Bosschaert said...

Hi Charles, yes I'm simply embedding the original Jersey jars (and their dependencies) and referring to them using bundle classpath.
I can't fully remember how I got to the list of imported and exported packages, but I think I generated it using Peter Kriens' BND or the maven-bundle-plugin.

Unknown said...

There is an example in Jersey distribution to show how to use OSGi Http Service with Jersey and Equinox. The only difference from the example in this blog post is it does not use Spring-DM, but it rather registers the Jersey Servlet directly in the OSGi bundle activator.
The example also includes a functional test based on Pax Exam framework.
The latest stable version of the example is available at [1]. After unzipping, you just need to uncomment the Equinox test exclude statement in functional-test/pom.xml



David Bosschaert said...

Great to hear that the Jersey distro now also supports OSGi!

Balazs Zsoldos said...


in the last couple of weeks I also created a simple solution that picks up any OSGi Service and starts them as Jersey Rest API-s under the /rest/ context path if they have the following service-properties:


This is basically a small part of the OSGi Remote Service specification. The annotations must be present on any of the interfaces the service object implements or on the service object itself (if the service object is proxied it is better to annotate an interface).

You can check this solution at:
And there is a sample project where the service is created via blueprint:

The difference is that this does not use any spring related classes only jersey and OSGI related classes.

Also as you do not have to pass class but object to Jersey you have the possibility to wire properties to your object in a way you like (e.g. with blueprint)