Skip to content
Mihail Kuznetsov edited this page Mar 16, 2015 · 2 revisions

Integration EverRest framework with Guice

Guice Integration

To integrate EverRest with Guice you need extend org.everrest.guice.servlet.EverrestGuiceContextListener class. You need implement method getModules() to instalse one or few modules with your JAX-RS components.

public class MyListener extends EverrestGuiceContextListener
{
   @Override
   protected List<Module> getModules()
   {
      List<Module> modules = new ArrayList<Module>(1);
      modules.add(new Module() {
         public void configure(Binder binder) {
            binder.bind(MyResource.class);
            binder.bind(MyProvider.class).in(Singleton.class);
         }
      });
      return modules;
   }
}

In example above MyResource is JAX-RS resource and it will work in per-request lifecycle and MyProvider is JAX-RS provider and will work in singleton lifecycle. Next, add following in web.xml :

<listener>
   <listener-class>org.everrest.example.MyListener</listener-class>
</listener>

Then you need add GuiceFiloter and mapping for it in you web.xml :

<filter>
   <filter-name>guiceFilter</filter-name>
   <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>guiceFilter</filter-name>
   <url-pattern>/**</url-pattern>
</filter-mapping>

Once you do it Guice and EverRest are set up. You don't need add EverRest servlet to you web.xml since it added programmatically in EverrestGuiceContextListener.

protected ServletModule getServletModule()
{
   return new ServletModule() {
      @Override
      protected void configureServlets() {
         serve("/**").with(GuiceEverrestServlet.class);
      }
   };
}

GuiceEverrestServlet is simple extention of org.everrest.core.servlet.EverrestServlet. It does nothing new just carry @com.google.inject.Singleton annotation since to requirement of Guice container (all servlets must be singleton components).

Example of EverRest and Guice integration can be found at subversion repository, see project everrest/everrest-integration/everrest-integration-guice-sample. You can simple run it with command mvn jetty:run. See details how-to try example in README.html in correspond project.