12

So i want to use Guice in Appengine with Cloud Endpoints to inject my services, or daos - pretty common I guess, but I found no tutorial for this.

Official Guice for Appengine documentation seems to be here: https://github.com/google/guice/wiki/GoogleAppEngine

When configuring Guice you set up the com.google.inject.servlet.GuiceFilter to intercept every request "/*". And at some point you must initialize the modules. Like the documentation says a good place to do that is a ServletContextListener.

One special kind of Module are ServletModules, that map request-Paths to Servlet-Classes, instead of doing this in the web.xml you can now do this programmatically.

Pretty straight forward up until here. But how do I configure Guice to also include the Endpoint-Classes?

Jonathan
  • 20,053
  • 6
  • 63
  • 70
icyerasor
  • 4,973
  • 1
  • 43
  • 52

1 Answers1

27

Turns out there is a GuiceSystemServiceServletModule that handles exactly this.

public class GuiceSSSModule extends GuiceSystemServiceServletModule {

  @Override
  protected void configureServlets() {
    super.configureServlets();

    Set<Class<?>> serviceClasses = new HashSet<Class<?>>();
    serviceClasses.add(MyEndpoint.class);
    serviceClasses.add(AnotherAndpoint.class);
    this.serveGuiceSystemServiceServlet("/_ah/spi/*", serviceClasses);
  }
}

Include this module in the Injector construction in your ServletContextListener:

public class MyGSCL extends GuiceServletContextListener {

  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new GuiceSSSModule(), new BaseModule());
  }
}

and use this listener in your web.xml:

<listener>
   <listener-class>de.mypkg.MyGSCL</listener-class>
</listener>

Also make sure to include the Guice filter in your web.xml:

<!-- GUICE -->
<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>

Your endpoints will be available under /_ah/api/... again and you can use @Inject in your endpoint classes.

icyerasor
  • 4,973
  • 1
  • 43
  • 52
  • 1
    This is the same way we use in our app engine backend. Since yesterday I could not update the backend anymore with app engine version 1.9.17 so I upgraded to 1.9.38. Now It uploads but I allways get this error: Uncaught exception from servlet java.lang.NoClassDefFoundError: nl/<..>/<..>/guice/GuiceSSSModule – Roel Jun 09 '16 at 13:36
  • @Roel I had a similar problem using app engine version 1.9.37, but after reading your comment, I upgraded to 1.9.46 and then problem vanished – Jorge P. Nov 13 '16 at 17:23