Appengine with Google Cloud Endpoints and Guice

2019-01-17 12:18发布

问题:

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?

回答1:

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.