Search

Chapter 36. Guice 1.0 Integration

download PDF
RESTEasy does have some basic integration with Guice 1.0. RESTEasy scans a Guice Module's binding types for @Path and @Provider annotations, and registers these bindings. The guice-hello project that comes in the RESTEasy examples/ directory gives a nice example of this.
@Path("hello")
public class HelloResource
{
   @GET
   @Path("{name}")
   public String hello(@PathParam("name") final String name) {
      return "Hello " + name;
   }
}
Start by specifying a JAX-RS resource class — in this case, the HelloResource. Next, create a Guice Module class that defines your bindings:
import com.google.inject.Module;
import com.google.inject.Binder;

public class HelloModule implements Module
{
    public void configure(final Binder binder)
    {
       binder.bind(HelloResource.class);
    }
}
Place these classes within your WAR WEB-INF/classes or in a JAR within WEB-INF/lib. Then, create your web.xml file. You will need to use the GuiceResteasyBootstrapServletContextListener like so:
<web-app>
    <display-name>Guice Hello</display-name>

    <context-param>
      <param-name>
        resteasy.guice.modules
      </param-name>
      <param-value>
        org.jboss.resteasy.examples.guice.hello.HelloModule
      </param-value>
    </context-param>

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

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
GuiceResteasyBootstrapServletContextListener is a subclass of ResteasyBootstrap, so you can use any other RESTEasy configuration option within your web.xml file. Also note the resteasy.guice.modules context parameter. This can take a comma delimited list of class names that are Guice Modules.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.