[Google Guice](https://github.com/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`](https://docs.cloud.google.com/appengine/docs/standard/java/config/webxml).

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.

> [!NOTE]
> **Note:** The service classes are not auto-detected by the plugins if not defined in `web.xml`.

### Maven


```xml
<dependency>
  <groupId>com.google.endpoints</groupId>
  <artifactId>endpoints-framework-guice</artifactId>
  <version>2.2.2</version>
</dependency>
```

### Gradle


```groovy
compile 'com.google.endpoints:endpoints-framework-guice:2.2.2'
```

```groovy
endpointsServer {
  // Endpoints Framework Plugin server-side configuration
  hostname = "${projectId}.appspot.com"
  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.

```xml
<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<!--
  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.
-->
<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/_ah/api/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>com.example.echo.EchoGuiceListener</listener-class>
</listener>
```

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

```java
public class EchoGuiceListener extends GuiceServletContextListener {

  @Override
  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:

```java
public class EchoEndpointModule extends EndpointsModule {
  @Override
  public void configureServlets() {
    super.configureServlets();

    bind(ServiceManagementConfigFilter.class).in(Singleton.class);
    filter("/_ah/api/*").through(ServiceManagementConfigFilter.class);

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

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

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

## What's next?

- [Write and annotate your backend API code](https://docs.cloud.google.com/endpoints/docs/frameworks/java/annotate-code.md.txt)
- [Learn more about annotations](https://docs.cloud.google.com/endpoints/docs/frameworks/java/annotations.md.txt)
- [Add API management](https://docs.cloud.google.com/endpoints/docs/frameworks/java/adding-api-management.md.txt)
- [Deploy and test your API](https://docs.cloud.google.com/endpoints/docs/frameworks/java/test-deploy.md.txt)
- [Learn more about supported parameter and return types](https://docs.cloud.google.com/endpoints/docs/frameworks/java/parameter-and-return-types.md.txt)