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

kenneth.westelinck@...
 

Hello,

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(
"org/glassfish/jersey/examples/osgihttpservice/spring/spring-context.xml"));

packages(false, "org.glassfish.jersey.examples.osgihttpservice.spring");
register(StatusResource.class);
}
}

I have created a pull request for this change:
https://github.com/jersey/jersey/pull/3768
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:
https://github.com/jersey/jersey/pull/174/files

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)?

Thanks.

Join jersey@javaee.groups.io to automatically receive all group messages.