Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appengine with Google Cloud Endpoints and Guice

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?

like image 901
icyerasor Avatar asked Jun 15 '13 23:06

icyerasor


People also ask

What are Google Cloud Endpoints?

Endpoints is an API management system that helps you secure, monitor, analyze, and set quotas on your APIs using the same infrastructure Google uses for its own APIs.

What is Endpoint Framework?

Endpoints Frameworks converts the return value to JSON and sends the response. You add metadata (by using annotations in Java and decorators in Python) to your source code. The metadata defines the surface of the REST APIs for your application. Java Python.

What is Gcp Framework?

The Google Cloud Architecture Framework provides recommendations and describes best practices to help architects, developers, administrators, and other cloud practitioners design and operate a cloud topology that's secure, efficient, resilient, high-performing, and cost-effective.

What is Gcloud App Engine?

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.


1 Answers

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.

like image 151
icyerasor Avatar answered Sep 23 '22 23:09

icyerasor