I tried building a sample RESTful app using Jersey 2.0/ HK2. The initial commit (as seen at https://github.com/venkatramachandran/restful-sample-tomcat/commit/6e5ee ) should have worked, given that the class to be injected was annotated with @Service
. But this did not.
I created a second commit ( https://github.com/venkatramachandran/restful-sample-tomcat/commit/2ca30f ) with a org.glassfish.jersey.server.ResourceConfig
instance which registers an instance of org.glassfish.hk2.utilities.binding.AbstractBinder
. The binder basically binds my @Contract
to @Service
which are basically the same class.
My question is, is a binder absolutely necessary for HK2 to work? Or did I miss some configuration in my initial commit which is causing the org.glassfish.hk2.api.UnsatisfiedDependencyException
?
Yes, you need to perform some sort of binding for HK2 to work for a standard @Service
like this. To prevent having to bind your services within Jersey you might want to consider bridging to an external ServiceLocator in order to supply your service though, as outlined in this question.
And of course its usually best practice for your @Contracts
to be interfaces, although obviously not mandatory.