Chapter 36. Guice 1.0 Integration
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.