【问题标题】:Google cloud endpoints and guice谷歌云端点和guice
【发布时间】:2014-07-05 14:10:09
【问题描述】:

我无法让谷歌云端点与谷歌 guice 一起工作。从端点库中可用的类看来,这应该是可能的,但我不清楚如何连接它,而且我没有看到任何文档。

显然有was a solution,但从那时起API似乎发生了变化。

我尝试扩展 com.google.api.server.spi.guice.SystemServiceModule 覆盖 configure() 和 getServiceClasses(),并实现 GuiceServletContextListener。

调用了 GuiceServletContextListener 上的 getInjector() 方法,因此调用了 SystemServiceModule 上的 configure() 方法,但从未调用过 "getServiceClasses() 方法。调用服务时,服务类没有注入任何依赖项。

有人知道如何正确连接吗?

【问题讨论】:

    标签: guice google-cloud-endpoints


    【解决方案1】:

    在 guice 中使用端点的关键是正确的 servlet 映射。试试看

    public class YourGuiceListener extends GuiceServletContextListener {
        static class ServletModule extends GuiceSystemServiceServletModule {
            @Override
            protected void configureServlets() {
                super.configureServlets();
                Set<Class<?>> serviceClasses = new HashSet<Class<?>>();
                serviceClasses.add(YourEndpointsService1.class);
                serviceClasses.add(YourEndpointsService2.class);
                this.serveGuiceSystemServiceServlet("/_ah/spi/*", serviceClasses);//endpoints servlet mapping
                ...
            }
        }
    
        public static class InjectionModule extends AbstractModule {
            @Override
            protected void configure() {
                bind... //optional bindings
            }
        }
    }
    

    将 Guice 监听器和过滤器添加到 web.xml

    <listener><listener-class>package.YourGuiceListener</listener-class></listener>
    <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
    

    【讨论】:

    • 在代码示例中搜索“你的”,你就会知道你需要在哪些点上钩。
    • 实现了提供两个/所有模块的 getInjector() 方法,它按预期工作。
    【解决方案2】:

    对于 Endpoints Framework v2,您可以扩展 EndpointsModule。

    将此依赖项添加到您的项目中:

    <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-framework-guice</artifactId>
        <version>2.0.9</version>
    </dependency>
    

    并像这样扩展模块:

    public class EchoEndpointModule extends EndpointsModule {
    
      @Override
      public void configureServlets() {
        super.configureServlets();
    
        bind(Echo.class).toInstance(new Echo());
        configureEndpoints("/_ah/api/*", ImmutableList.of(Echo.class));
      }
    }
    

    完整示例:

    https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine/endpoints-frameworks-v2/guice-example

    随附文件: https://cloud.google.com/endpoints/docs/frameworks/java/using-guice

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2016-01-20
      相关资源
      最近更新 更多