Skip to content
Advertisement

Jersey 1 @Inject migrated to Jersey 2 stopped working

I am doing a Jersey 1 to Jersey 2 migration of my system. In my code I had @Inject annotation from com.sun.jersey.spi.inject.Inject and @Singleton from com.sun.jersey.spi.resource.Singleton. I’ve changed these to javax.inject.Inject and javax.inject.Singleton.

Since this change I am getting errors while injecting any object annotated with it. My error is this

javax.servlet.ServletException: A MultiException has 4 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=BarcodeSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1090224052)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=RSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1247004825)
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.search.barcode.BarcodeSearchClient errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.search.barcode.BarcodeSearchClient

    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:423)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    com.filter.MetricsEmittingFilter.doFilter(MetricsEmittingFilter.java:89)

The APIConnectorHandler is Injected both in RSearchClient and BarcodeSearchClient. I first called the endpoint hitting RSearchClient in the flow and then BarcodeSearchClient because of which RSearchClient is also shown as exception #2.

My web.xml looks like this

<?xml version="1.0"?>
<!--
When modifying this file, DO NOT FORGET to also modify workspace-web.xml
if necessary so that "brazil-build server" will work.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <description>Spring Expression Language Support</description>
        <param-name>springJspExpressionSupport</param-name>
        <param-value>false</param-value>
    </context-param>
    <listener>
        <listener-class>com.abc.listener.ContextListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>JerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                com.search, com.fasterxml.jackson.jaxrs.json
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <filter-mapping>
        <filter-name>MetricsEmittingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

A few things i did find what that Hk2 is unable to find the beans and stuff but what i’m unable to understand is how did it work in Jersey 1. Where was Jersey 1 @Inject getting the beans from?

PS. This is a super legacy code and i don’t really know why but it has spring integration but none of the objects are created as beans. They are just initialized with new in multiple places. Spring is used to initialize only region specific beans.

Advertisement

Answer

Got it working. I had to create a binder class and app class and link it in web.xml. The binder class needed to have binding of all the classes i’m injecting via @Inject and their dependencies.

Followed https://www.appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement