Home > Java > RESTEasy and OSGi: Perfect match

RESTEasy and OSGi: Perfect match

I used to develop Eclipse plugins which have been implemented since version 3.0 as OSGi bundles. Also, it is possible to run OSGi bundles inside a web container allowing these bundles to provide services and resources to a web application providing an outstanding modular platform.

As I’m a big fan of jBoss and of jBoss RESTEasy (JAX-RS implementation) I started looking for a solution to integrate these frameworks and build a web application based on REST services and bundles.

How things get together

First of all, you need to run an OSGi framework inside a web container. Today there are many possibilities however, I choose to use the Eclipse Equinox bridge servlet. You can find more about this implementation here.

Second, you need to convert the jBoss RESTEasy libraries and dependencies into OSGi bundles. You can easy create these bundles by creating Eclipse plugins from existent jar archives (Eclipse: File -> new -> Other, Plug-in form existent JAR archives) and follow the wizard to create a bundle adding the RESTEasy jar files.

Third, we need to use my plugin to start the RESTEasy framework inside the web container as an OSGi service.

And them, you start creating your JAX-RS resources as bundles.


Create a new Eclipse Plug-in project (Tip: In the wizard select an OSGi framework as Standard). You can download an example plugin here.

Sample Activator class

package sample1;

import org.jboss.resteasy.plugins.server.osgi.IResteasyService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {

 private ServiceTracker serviceTracker;
 private IResteasyService service;

 public void start(BundleContext context) throws Exception {

    serviceTracker = new ServiceTracker(context, IResteasyService.SERVICE_NAME, null);

    service = (IResteasyService) serviceTracker.getService();

    if(service != null) {
       System.out.println("Registering singleton");
       service.addSingletonResource(new SampleResource());
       System.out.println(SampleResource.class.getName() + " registered");

 public void stop(BundleContext context) throws Exception {

    System.out.println("Removing singleton");

    serviceTracker = null;

Sample JAX-RS Resource class

package sample1;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;

public class SampleResource2 {

 public String sayHello() throws WebApplicationException {
    return "Hello from OSGi";

If you start this bundle your will be able to access the url http://localhost:8080/osgi/rest/sample2/hello, and if you stop this bundle the path /sample2/hello will be no longer available for client requests. In this implementation the path /osgi is the application context and /rest is the RESTEasy mapping prefix from the web.xml file.

Download here a complete WAR file to deploy this solution and test yourself the power of RESTEasy and OSGi together.

Download here the experimental code develop to connect the RESTEasy and OSGi.

Categories: Java Tags: , , , , ,
  1. Alessandra
    May 16, 2010 at 4:41 pm

    Hello David!
    Thought I should say hello and check if you are okay. Must be wonderful to be on vacation (I’m green of envy). How’s everything?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: