Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTEasy Guice Provider

I'm having a small problem when trying to use Guice with ContainerRequestFilter, it throws a NullPointerException. I did a little digging into RESTEasy and it would appear that it can't find a constructor for MyFilter due to the @Context annotation not being present, the NullPointerException is thrown when trying to instantiate a null constructor.

My filter:

@Provider
@PreMatching
public class MyFilter implements ContainerRequestFilter {
    private Dependency d;

    @Inject
    public MyFilter(Dependency d) {
        this.d = d;
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        if (d.doSomething()) {
            Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
            containerRequestContext.abortWith(r);
        }
    }
}

I've added the filter to my Application class:

@ApplicationPath("")
public class Main extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> c = new HashSet<Class<?>>();

    public Main() {
        c.add(Dependency.class);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return c;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

My Guice configuration:

public class GuiceConfigurator implements Module {
    public void configure(final Binder binder) {
        binder.bind(Dependency.class);
    }
}

My web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>My App</display-name>

    <context-param>
        <param-name>resteasy.guice.modules</param-name>
        <param-value>com.example.GuiceConfigurator</param-value>
    </context-param>

    <listener>
        <listener-class>
          org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
        </listener-class>
    </listener>
</web-app>

This configuration is working for injecting my dependencies into resources, but I get a NullPointerException when trying to use it on a provider.

Any help would be appreciated.

like image 407
jmahony Avatar asked Apr 11 '16 17:04

jmahony


1 Answers

It seems that even with RESTeasy/JAX-RS components, you still need to register it with the Guice binder. I wasn't sure at first, but looking at the test cases, it seems we still need to register our resources and providers with Guice to make it work.

I test it after adding the filter to the Guice module, and it works as expected.

public class GuiceConfigurator implements Module {
    public void configure(final Binder binder) {
        binder.bind(MyFilter.class);
        binder.bind(Dependency.class);
    }
}

To test, I went off the example from the RESTeasy project, added a filter with the constructor injection, and added the filter to the module binder. And it worked when adding the filter to the module and failed when not added.

like image 184
Paul Samsotha Avatar answered Nov 20 '22 06:11

Paul Samsotha