Using Guice with Cloud Endpoints Frameworks

Google Guice is a dependency injection framework that you can use with an Endpoints Frameworks v2 project to configure servlet mapping and filtering programmatically in Java, rather than in web.xml.

To use Guice, you need to add the following prepackaged dependency to your pom.xml or build.gradle. Also, you need to configure the Endpoints Frameworks plugins for Maven and Gradle to define which service classes the plugins use to create OpenAPI documents.




compile ''
endpointsServer {
  // Endpoints Framework Plugin server-side configuration
  hostname = "${projectId}"
  serviceClasses = ['com.example.echo.Echo']

Next, you need to update web.xml to direct all traffic from /_ah/api/* to the Endpoints Frameworks Guice Servlet.


  URL Pattern /_ah/api/* instead of /* because a legacy v1 servlet uses
  the route /_ah/api/ and using /* will erronously use the legacy v1
  servlet instead of routing to your API.


Implement the listener class in your project. It should look similar to the following depending on the number of services:

public class EchoGuiceListener extends GuiceServletContextListener {

  protected Injector getInjector() {
    return Guice.createInjector(new EchoEndpointModule());

The listener class creates a new injector that handles servlet mapping and filtering, which is normally defined by the web.xml, but instead is now defined by the EchoEndpointModule class defined as:

public class EchoEndpointModule extends EndpointsModule {
  public void configureServlets() {


    Map<String, String> apiController = new HashMap<>();
    apiController.put("endpoints.projectId", "YOUR-PROJECT-ID");
    apiController.put("endpoints.serviceName", "");

    filter("/_ah/api/*").through(GoogleAppEngineControlFilter.class, apiController);

    bind(Echo.class).toInstance(new Echo());
    configureEndpoints("/_ah/api/*", ImmutableList.of(Echo.class));

What's next?