users@jersey.java.net

[Jersey] Re: CDI Injection into JAX-RS 2 Filter

From: algermissen1971 <algermissen1971_at_mac.com>
Date: Thu, 01 Aug 2013 16:17:54 +0200

Jakub,

On 01.08.2013, at 16:01, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:

> Hi Jan,
>
> AbstractBinder has been moved to HK2 (See [1]),
> the API should be more-less the same. So you can stick with
> current jersey-common.

Ah, thanks. Still, seems odd I'd have to just know such a thing when maintaining a 'normal' application. (But see below).

>
> Otherwise CDI injection should be working for just fine in GF.
> I am wondering why you need a custom binder. Could you please
> provide some more details on your use case. Maybe this is a bug in Jersey.

No idea. I have been struggeling with this issue ever since. I'll give out-of-the-box CDI another try and report back. Maybe it *was* just a bug in Jersey/GF and it has gone away.

Meanwhile, thanks.

Jan


>
> Thanks,
>
> ~Jakub
>
>
> [1]https://hk2.java.net/hk2-api/apidocs/org/glassfish/hk2/utilities/binding/AbstractBinder.html
>
> On Aug 1, 2013, at 3:38 PM, algermissen1971 <algermissen1971_at_mac.com> wrote:
>
>> 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
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>