使用Google Cloud Endpoints和Guice进行播放

时间:2020-12-26 23:13:38

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.

所以我想在Appengine中使用Guice和Cloud Endpoints来注入我的服务或者daos - 我猜这很常见,但是我找不到这方面的教程。

Official Guice for Appengine documentation seems to be here: https://github.com/google/guice/wiki/GoogleAppEngine

官方Guice for Appengine文档似乎在这里: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.

配置Guice时,您需要设置com.google.inject.servlet.GuiceFilter来拦截每个请求“/ *”。在某些时候,您必须初始化模块。就像文档说的那样,一个好的地方就是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.

一种特殊的模块是ServletModules,它将请求路径映射到Servlet-Classes,而不是在web.xml中执行此操作,您现在可以通过编程方式执行此操作。

Pretty straight forward up until here. But how do I configure Guice to also include the Endpoint-Classes?

非常直接,直到这里。但是如何配置Guice还包括Endpoint-Class?

1 个解决方案

#1


26  

Turns out there is a GuiceSystemServiceServletModule that handles exactly this.

事实证明,有一个GuiceSystemServiceServletModule可以处理这个问题。

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:

在ServletContextListener的Injector构造中包含此模块:

public class MyGSCL extends GuiceServletContextListener {

  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new GuiceSSSModule(), new BaseModule());
  }
}

and use this listener in your web.xml:

并在web.xml中使用此侦听器:

<listener>
   <listener-class>de.mypkg.MyGSCL</listener-class>
</listener>

Also make sure to include the Guice filter in your web.xml:

还要确保在web.xml中包含Guice过滤器:

<!-- 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.

您的端点将再次位于/ _ah / api / ...下,您可以在端点类中使用@Inject。

#1


26  

Turns out there is a GuiceSystemServiceServletModule that handles exactly this.

事实证明,有一个GuiceSystemServiceServletModule可以处理这个问题。

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:

在ServletContextListener的Injector构造中包含此模块:

public class MyGSCL extends GuiceServletContextListener {

  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new GuiceSSSModule(), new BaseModule());
  }
}

and use this listener in your web.xml:

并在web.xml中使用此侦听器:

<listener>
   <listener-class>de.mypkg.MyGSCL</listener-class>
</listener>

Also make sure to include the Guice filter in your web.xml:

还要确保在web.xml中包含Guice过滤器:

<!-- 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.

您的端点将再次位于/ _ah / api / ...下,您可以在端点类中使用@Inject。