users@jersey.java.net

[Jersey] CDI Injection into JAX-RS 2 Filter

From: algermissen1971 <algermissen1971_at_mac.com>
Date: Thu, 01 Aug 2013 15:38:59 +0200

Hi all,

I am again struggling with the question of how to enable CDI for JAX-RS 2 filter classes.

Marek (IIRC) has pointed me the solution based on AbstractBinder:

Suppose I want to inject in a filter the instance of MyToBeInjectedSingleton.class like so:

class MyFilter .... {

        @Inject
        MyToBeInjectedSingleton mySingleton;

}

I was told to extend AbstratBinder:

public class MyBinder extends AbstractBinder {
        
        @Override
        protected void configure() {

                bindAsContract(MyToBeInjectedSingleton.class).in(Singleton.class);
        
        }


}

and then register the Binder in a Feature:

public class MyFeature implements Feature {

        @Override
        public boolean configure(FeatureContext featureContext) {
            featureContext.register(new MyBinder());
                return true;
        }
}

Worked fine, but it seems the API of jersey-common changed and AbstractBinder is gone. Now, I could pull in
the older jersey-common, but as the code is to run in GF4 (which comes with the new version), I guess this would lead to serious conflicts somewhere.

Hence the question: what do I need to do in the new jersey-common to register an instance for injection?


Besides that, I still cannot really understand that one needs to go through this kind of trouble (with changing APIs) just because I want to inject something in a JAX-RS component. Isn't there a better way? Maybe I am just too stupid or blind to see the light.



Jan