Can't inject the guice dependency in the jersey filter

214 views Asked by At

In the process of setup a bridge between guice and jersey, I ran into one problem. When trying to create a jersey filter, I was unable to inject guice dependencies into it.

I found a duplicate, however there is no solution to the problem there. Everything is exactly the same. The only difference is that I don't get a startup error. The filter works, but my dependencies are null.

Interestingly, Filter and HttpFilter work fine. But it doesn't really work for me. There's another thing that's interesting. In the resource, which I understand is an HK2 dependency, I can inject guice bean.

@ApplicationPath("/test")
private static class TestApplicationConfig extends ResourceConfig
{

    public TestApplicationConfig()
    {
        register(JacksonFeature.class);
        register(AuthFilter.class);

        register(new ContainerLifecycleListener()
        {
            public void onStartup(Container container)
            {
                ServletContainer servletContainer = (ServletContainer) container;
                ServiceLocator serviceLocator = container.getApplicationHandler().getServiceLocator();
                GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
                GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
                Injector injector = (Injector) servletContainer
                                                   .getServletContext()
                                                   .getAttribute(Injector.class.getName());
                guiceBridge.bridgeGuiceInjector(injector);
            }

            public void onReload(Container container)
            {
            }

            public void onShutdown(Container container)
            {
            }
        });
    }
}

In ServletModule child.

serve(path).with(ServletContainer.class, ImmutableMap.of(
            "javax.ws.rs.Application", TestApplicationConfig.class.getName(),
            "jersey.config.server.provider.packages", sb.toString()));

I trying with register(AuthFilter.class) and @Provider

@Singleton
@Provider
public class AuthFilter implements ContainerRequestFilter
{

@Inject
private SomeInjectedService someInjectedService; **// null here**

@Context
private ResourceInfo resourceInfo;

@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
    // some code
}
}

SomeInjectedService I register by guice

bind(SomeInjectedService.class).asEagerSingleton();

Where can I start diagnosing and what can I do?

UPD:

I noticed different behavior when using different annotations.

If I use javax.inject.Inject, I get the following error message.

org.glassfish.hk2.api.MultiException: A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SomeInjectedService,parent=AuthFilter,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1496814489)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of some.package.AuthFilter errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on some.package.AuthFilter

If com.google.inject.Inject, just null. As I understand this method is not correct.

Considering that javax Inject is trying to inject the service but can't find it. Can we conclude that the bridge is not working correctly? But if it's not working correctly, why can I inject this service into my resource?

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SomeResource
{

    private final SomeInjectedService someInjectedResource;

    @Inject // here I use javax annotation and this code working correctry
    public SomeResource(SomeInjectedService someInjectedResource)
    {
        this.someInjectedResource = someInjectedResource;
    }

    @GET
    @Path("/{user}")
    public Response returnSomeResponse(@PathParam("user") String user) throws Exception
    {
        // some code
    }

}
0

There are 0 answers