Search code examples
jax-rsconstructor-injection

How to resolve construct based injection in Jax-RS


I am in jax-rs and write to write simple Test API. While doing this facing difficulties to resolve construct based injection.

Following is service configuration. In which 2 services and 1 decorator class. TestImpl and EnglishTestDecorator service inherit same interface and have their name as well.

public class ApplicationBinder extends AbstractBinder {
    bind(TestImpl.class).to(TestService.class).named("testImpl").in(Singleton.class);
    bind(ReportImpl.class).to(ReportService.class).named("reportImpl").in(Singleton.class);
    bind(EnglishTestDecorator.class).to(TestService.class).named("englishTestDecorator").in(Singleton.class);
}

Here is Decorator class where injecting two dependencies at construction level. This class giving error.

public class EnglishTestDecorator implements TestService {
    private TestImpl testImpl;
    private ReportImpl reportImpl

    @Inject
    public EnglishTestDecorator(TestImpl testImpl, ReportImpl reportImpl){
        this.testImpl = testImpl;
        this.reportImpl = reportImpl;
    }

}

Here is error : Can someone help to resolve this ?

{"@version":1,"message":"//rest:8080/api/test/testReport/","logger_name":"org.eclipse.jetty.server.HttpChannel","thread_name":"qtp366590980-14","level":"WARN","level_value":30000,"stack_trace":"javax.servlet.ServletException: javax.servlet.ServletException: A MultiException has 6 exceptions. They are:\n1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=TestImpl,parent=EnglishTestDecorator,qualifiers={},position=0,optional=false,self=false,unqualified=null,40643858)\n2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ReportImpl,parent=EnglishTestDecorator,qualifiers={},position=1,optional=false,self=false,unqualified=null,1216092469)\n3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.test.service.decorator.EnglishTestDecorator errors were found\n4. java.lang.IllegalStateException: Unable to perform operation: resolve on org.test.service.decorator.EnglishTestDecorator\n5. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.cas.osd.cst.resource.SSOSupportResource errors were found\n6. java.lang.IllegalStateException: Unable to perform operation: resolve on org.cas.osd.cst.resource.SSOSupportResource\n\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:139)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:524)\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)\n\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)\n\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)\n\tat org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)\n\tat


Solution

  • I did lot of research on this and found its JAX-RS limitation it can not resolve to concrete class that's why inject was failing. Following is new piece of code that worked for me.

    @Inject public EnglishTestDecorator(@Name("testImpl") TestService testImpl, @Named("reportImpl") ReportServicer eportImpl){ }