2

I'm using jersey 2.17 on tomcat 8, and I'm having a problem with what I think should be a very straightforward and common use case.

I have various beans defined in a spring XML file. I've now annotated one of them to expose some methods as rest APIs. This is a singleton bean, with a reference to another bean injected in the spring XML configuration.

My rest APIs are working, but the problem is that every time one is invoked a new instance of the bean class is created, and my bean reference is null. My understanding is that this is because jersey is managing the lifecycle instead of spring, and I need SpringComponentProvider, which will then provide the singleton.

I included jersey-spring3-2.17.jar in my application, and I specified the spring package in the servlet configuration in web.xml, as follows:

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>my.rest.package;org.glassfish.jersey.server.spring</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.tracing.type</param-name>
            <param-value>ALL</param-value>
        </init-param>        
        <load-on-startup>1</load-on-startup>
    </servlet>

I know that the jersey.config.server.provider.packages parameter is being picked up because the annotated class in my.rest.package gets found, but the SpringComponentProvider is apparently not being registered, because I'm still getting a new instance of my bean every time.

I tried specifying the class explicitly like this:

    <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.spring.SpringComponentProvider</param-value>
    </init-param>

That didn't change the behavior either.

Any insights or debugging suggestions appreciated.

UPDATE: I realized that the version of my spring jars was behind the version expected by my jersey jars, and I think that's the problem. Once I'm able to update spring, if that fixes it I'll close this.

2 Answers 2

1

Jersey will create a new instance on each call regardless if you have an XML bean of the same class as singleton in spring context:

https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2331

Also see this response on how to change that behavior:

https://stackoverflow.com/a/14739010/2879838

Within your Jersey Resource class, since you already added the jersey-spring dependency, you can simply add the @Autowire annotation on your member variable that you say is injected via XML file in spring. This will spring-inject the dependency when Jersey instantiates the resource on each request.

Hope this helps.

1
  • Autowire does solve the injection problem, but I have a resource that does need to be singleton, and Singleton did not fix it. However your answer did lead me down another line of thought and I found the solution.
    – Dana
    Aug 7, 2015 at 1:23
1

When I finally was able to update my spring, it turned out that was not the issue. I had to add @Component as well as @Singleton to my resource in order for jersey to recognize it as a spring-managed singleton. This problem would never have arisen if I was using annotations for spring; it happened because I was using XML to create the spring beans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.