Jersey JAX-RS Dependency Injection

Asked by At

I'm trying to build a REST API using JAX-RS, specifically the Jersey implementation. I tried to implement Authentication like in this StackOverflow question.

Basically I'm using a ContainerRequestFilter to filter the HTTP Requests, based on the Authorization header. This class defines a property, annotated with the @Inject attribute (I'm using Kotlin, not Java):

@Inject
var authenticationService: IAuthenticationService? = null

I've registered this authentication service in my AbstractBinder:

class MyApplicationBinder : AbstractBinder() {
    override fun configure() {
        bind(AuthenticationService::class.java) to IAuthenticationService::class.java
    }
}

and registering this application binder to my ResourceConfig:

class MyResourceConfig : ResourceConfig() {
    init {
        // ...
        register(MyApplicationBinder())
    }
}

However I'm getting the runtime exception on startup:

org.glassfish.hk2.api.UnsatisfiedDependencyException:
    There was no object available for injection at SystemInjecteeImpl(
        requiredType=IAuthenticationService,
        parent=AuthenticationRequestFilter,
        qualifiers={},
        position=-1,
        optional=false,
        self=false,
        unqualified=null,
        452444366
    )

I have no idea why the registration in my AbstractBinder is ignored by the HK2 Container! Can someone please help me out? Thanks!

0 Answers