[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eGoogle Guice is a dependency injection framework used in Endpoints Frameworks v2 projects to configure servlet mapping and filtering programmatically in Java.\u003c/p\u003e\n"],["\u003cp\u003eUsing Guice requires adding the \u003ccode\u003eendpoints-framework-guice\u003c/code\u003e dependency to your project's \u003ccode\u003epom.xml\u003c/code\u003e or \u003ccode\u003ebuild.gradle\u003c/code\u003e file and configuring the Endpoints Frameworks plugins for Maven and Gradle.\u003c/p\u003e\n"],["\u003cp\u003eYou need to update \u003ccode\u003eweb.xml\u003c/code\u003e to redirect all traffic from \u003ccode\u003e/_ah/api/*\u003c/code\u003e to the Endpoints Frameworks Guice Servlet, using the GuiceFilter class.\u003c/p\u003e\n"],["\u003cp\u003eAn \u003ccode\u003eEchoGuiceListener\u003c/code\u003e class must be implemented to create an injector that handles servlet mapping and filtering, replacing the need for manual \u003ccode\u003eweb.xml\u003c/code\u003e configuration.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eEchoEndpointModule\u003c/code\u003e class is used to define the configurations for the servlets, including the project ID and service name, as well as binding and filtering using classes like \u003ccode\u003eServiceManagementConfigFilter\u003c/code\u003e and \u003ccode\u003eGoogleAppEngineControlFilter\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Using Guice with Cloud Endpoints Frameworks\n\n[Google Guice](https://github.com/google/guice)\nis a dependency injection framework that you can use with an\nEndpoints Frameworks v2 project to configure servlet mapping and filtering\nprogrammatically in Java, rather than in\n[`web.xml`](/appengine/docs/standard/java/config/webxml).\n\nTo use Guice, you need to add the following prepackaged dependency to your\n`pom.xml` or `build.gradle`. Also, you need to configure the\nEndpoints Frameworks plugins for Maven and Gradle to define which\nservice classes the plugins use to create OpenAPI documents.\n**Note:** The service classes are not auto-detected by the plugins if not defined in `web.xml`. \n\n### Maven\n\n\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.endpoints\u003c/groupId\u003e\n \u003cartifactId\u003eendpoints-framework-guice\u003c/artifactId\u003e\n \u003cversion\u003e2.2.2\u003c/version\u003e\n \u003c/dependency\u003e\n\n### Gradle\n\n\n compile 'com.google.endpoints:endpoints-framework-guice:2.2.2'\n\n endpointsServer {\n // Endpoints Framework Plugin server-side configuration\n hostname = \"${projectId}.appspot.com\"\n serviceClasses = ['com.example.echo.Echo']\n }\n\nNext, you need to update `web.xml` to direct all traffic from `/_ah/api/*`\nto the Endpoints Frameworks Guice Servlet. \n\n \u003cfilter\u003e\n \u003cfilter-name\u003eguiceFilter\u003c/filter-name\u003e\n \u003cfilter-class\u003ecom.google.inject.servlet.GuiceFilter\u003c/filter-class\u003e\n \u003c/filter\u003e\n\n \u003c!--\n URL Pattern /_ah/api/* instead of /* because a legacy v1 servlet uses\n the route /_ah/api/ and using /* will erronously use the legacy v1\n servlet instead of routing to your API.\n --\u003e\n \u003cfilter-mapping\u003e\n \u003cfilter-name\u003eguiceFilter\u003c/filter-name\u003e\n \u003curl-pattern\u003e/_ah/api/*\u003c/url-pattern\u003e\n \u003c/filter-mapping\u003e\n\n \u003clistener\u003e\n \u003clistener-class\u003ecom.example.echo.EchoGuiceListener\u003c/listener-class\u003e\n \u003c/listener\u003e\n\nImplement the listener class in your project. It should look similar to the\nfollowing depending on the number of services: \n\n public class EchoGuiceListener extends GuiceServletContextListener {\n\n @Override\n protected Injector getInjector() {\n return Guice.createInjector(new EchoEndpointModule());\n }\n }\n\nThe listener class creates a new injector that handles servlet mapping and\nfiltering, which is normally defined by the `web.xml`, but instead is now\ndefined by the EchoEndpointModule class defined as: \n\n public class EchoEndpointModule extends EndpointsModule {\n @Override\n public void configureServlets() {\n super.configureServlets();\n\n bind(ServiceManagementConfigFilter.class).in(Singleton.class);\n filter(\"/_ah/api/*\").through(ServiceManagementConfigFilter.class);\n\n Map\u003cString, String\u003e apiController = new HashMap\u003c\u003e();\n apiController.put(\"endpoints.projectId\", \"YOUR-PROJECT-ID\");\n apiController.put(\"endpoints.serviceName\", \"YOUR-PROJECT-ID.appspot.com\");\n\n bind(GoogleAppEngineControlFilter.class).in(Singleton.class);\n filter(\"/_ah/api/*\").through(GoogleAppEngineControlFilter.class, apiController);\n\n bind(Echo.class).toInstance(new Echo());\n configureEndpoints(\"/_ah/api/*\", ImmutableList.of(Echo.class));\n }\n }\n\nWhat's next?\n------------\n\n- [Write and annotate your backend API code](/endpoints/docs/frameworks/java/annotate-code)\n- [Learn more about annotations](/endpoints/docs/frameworks/java/annotations)\n- [Add API management](/endpoints/docs/frameworks/java/adding-api-management)\n- [Deploy and test your API](/endpoints/docs/frameworks/java/test-deploy)\n- [Learn more about supported parameter and return types](/endpoints/docs/frameworks/java/parameter-and-return-types)"]]