Este conteúdo não está disponível no idioma selecionado.

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;
   }
}
Copy to Clipboard Toggle word wrap
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);
    }
}
Copy to Clipboard Toggle word wrap
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>
Copy to Clipboard Toggle word wrap
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.
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat