Using Jersey 2.x in an OSGi container, together with spring



We're trying to use Jersey 2.x in our application running in an OSGi container. Our application also uses Spring for DI. Thanks to the long list of examples, available in the source code, I was able to accomplish this quite easily.

The problem is, however, combining Jersey 2.x, OSGi and Spring does not work out of the box. In the initialize step of the SpringComponentProvider, the code tries to fetch the application context from the web application context.
Here's the relevant code:

ServletContext sc = injectionManager.getInstance(ServletContext.class);

if (sc != null) {
// servlet container
ctx = WebApplicationContextUtils.getWebApplicationContext(sc);

This does not work, and ctx remains null. So I added this:

if (ctx == null) {
// still null, try to create one
ctx = createSpringContext();

And I pass the application context through the "contextConfig" property:

public class JerseyApplication extends ResourceConfig {

public JerseyApplication() {
property("contextConfig", new ClassPathXmlApplicationContext(

packages(false, "org.glassfish.jersey.examples.osgihttpservice.spring");

I have created a pull request for this change:
I have also modified the osgi-http-service functional test, which now includes a resource with an injected bean.
We have also stumbled upon an issue where @Resource is not supported. We found the following outdated PR that seems to fix this for us:

Do I need to add additional things in my PR? If I want to have the @Resource annotation available, should I also include this in my PR (since the original PR is not valid anymore)?


Join to automatically receive all group messages.