users@jersey.java.net

[Jersey] Custom jersey class member injection exception

From: Alperovitch, Yasmine <Yasmine.Alperovitch_at_emc.com>
Date: Wed, 21 Jan 2015 15:02:11 +0000

I have created a custom injectable provider, I've tried 2 approaches:

1)

@Provider

public static final class PrincipleInjectableProvider extends PerRequestTypeInjectableProvider<Context, Principal>

{

    public PrincipleInjectableProvider()

    {

        super(Principal.class);

    }



    @Override

    public Injectable<Principal> getInjectable(final ComponentContext ic, final Context a)

    {

        return new AbstractHttpContextInjectable<Principal>() {



            @Override

            public Principal getValue(final HttpContext c)

            {

                return c.getRequest().getUserPrincipal();

            }

        };

    }



}

2)

@Provider

public static final class PrincipleInjectableProvider extends AbstractHttpContextInjectable<Principal>

    implements InjectableProvider<Context, Type>, Injectable<Principal>

{



    @Override

    public ComponentScope getScope()

    {

        return ComponentScope.PerRequest;

    }



    @Override

    public Injectable<Principal> getInjectable(final ComponentContext ic, final Context a, final Type c)

    {

        return this;

    }



    @Override

    public Principal getValue(final HttpContext c)

    {

        return c.getRequest().getUserPrincipal();

    }

}

I made sure to add this class to my resource config:

final ResourceConfig resourceConfig = new DefaultResourceConfig(PrincipleInjectableProvider.class);

and in my REST resource I try to inject the class like this:

@Context

private Principal _principal;

I was originally planning on using a separate annotation called @Principal, but I would settle right now on making this work.

When I was trying to inject this as a method param, it was successful (with some changes to the injectable class, to implement

InjectableProvider<Context, Parameter>

instead)

but I just can't seem to inject it as a class member..

This is the exception message I'm getting:

Missing dependency for field: private com.....principal.Principal com.....net.TestResource._principal



Please help me find what I've done wrong here, I tried with jersey 1.12 and 1.18.
Thanks,
Jasmine