users@jersey.java.net

[Jersey] Injection of generics

From: Trenton D. Adams <trenton.d.adams_at_gmail.com>
Date: Thu, 17 Mar 2016 00:10:49 -0600

Good day,

I'd like to inject his...

@Inject
protected ParameterHandler<SampleParameters> pageParameters;

With a factory like so...

public class ParameterHandlerFactory
    implements Factory<ParameterHandler>
{

    private final HttpServletRequest request;

    @Inject
    public ParameterHandlerFactory(final HttpServletRequest request)
    {
        this.request = request;
    }

    @Override
    public ParameterHandler provide()
    {
        return new ParameterHandler(request,
            SampleParameters.class);
    }

    @Override
    public void dispose(
        final ParameterHandler parameterEnumParameterHandler)
    {

    }

}

And it's registered like so...

register(new AbstractBinder()
{
    @Override
    protected void configure()
    {
        bindFactory(ParameterHandlerFactory.class).to(
            ParameterHandler.class);
    }
});


And I did in fact originally have the Factory taking the same generic, but
that didn't work either. The errors are below, with most of the actual
stack trace removed for brevity.

But it's not working....
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object
available for injection at
SystemInjecteeImpl(requiredType=ParameterHandler<SampleParameters>,parent=ParametersSample,qualifiers={},position=-1,optional=false,self
=false,unqualified=null,2004350963)

MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the
dependencies of com.example.ParametersSample errors were found

MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on
com.example.ParametersSample

As soon as I remove the generic part of it, it works great.

I'm new to generics, so this exercise may be pointless, in that I'm not
sure how to get the class of a generic so that I don't have to hardcode
that "SampleParameters" class into the factory. I'm mainly curious if
there's something obvious to someone that has been using generics for
awhile; or maybe HK2 doesn't support this?