在Java GAE后端Servlet中使用Guice Injection

时间:2012-12-20 03:44:42

标签: google-app-engine servlets guice guice-servlet sitebricks

我的GAE Java应用程序陷入困境:我正在尝试编写一个将由GAE后端处理的servlet 它还必须使用 Guice依赖注入来启动所有servlet的字段。我尝试了多种方法,但似乎没有人为我做这个伎俩。

我首先尝试在servlet中使用 Sitebricks ,但似乎这样做导致GAE在查询后端URL时无法将该类检测为合法的servlet:a 404 Not Found 始终从后端返回。尽管如此,我确信该类已使用Sitebricks注释正确注释,因为相同的URI路径可以与前端实例一起正常工作。

接下来,我尝试使用 Guice Servlet Extension (参见this link);在我创建全局Injector的类中,我使用了这个绑定:

return Guice.createInjector(..., new ServletModule() {
    @Override
    protected void configureServlets() {
        serve("/backend/*").with(MyBackend.class);
    }
}, ...);

不幸的是,与Sitebricks发生的问题完全相同:我在后端返回404错误,但在前端没有。

最后一种可行的方法是在web.xml中配置servlet-mapping;传统的方法。这最终允许我的后端检测URI映射(后面跟着Google's documentation of Java GAE backends中写的内容:“后端与主应用程序版本共享web.xml中定义的servlet。”) 。不幸的是,我无法使用此程序来利用Guice注入,因此使用web.xml非常不合适。

我甚至尝试结合第二种和第三种方法:同时使用Guice Servlet Extension和web.xml。这个错误从后端返回500错误,并出现此错误:

java.lang.InstantiationException: com.example.MyBackend
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.BackendServersFilter.doRedirectedServerRequest(BackendServersFilter.java:292)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:106)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at com.google.appengine.tools.development.BackendServers.forwardToServer(BackendServers.java:56)
    at com.google.appengine.tools.development.BackendServersFilter.doServerRedirect(BackendServersFilter.java:237)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:100)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:383)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

20-Dec-2012 3:23:53 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: /backend/example/rest/call
java.lang.InstantiationException: com.example.MyBackend
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.BackendServersFilter.doRedirectedServerRequest(BackendServersFilter.java:292)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:106)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at com.google.appengine.tools.development.BackendServers.forwardToServer(BackendServers.java:56)
    at com.google.appengine.tools.development.BackendServersFilter.doServerRedirect(BackendServersFilter.java:237)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:100)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:383)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

最后要注意的一点是:我将 backends.xml 文件配置为<public>true</public>,但是我通过从GAE应用程序的前端查询后端来测试这些HTTP请求调用。另请注意,我的前端实例正在使用BackendServicejavadoc here)正确检索后端地址。

我之前提出的其他相关Stackoverflow问题

1 个答案:

答案 0 :(得分:1)

Backends have been deprecated赞成Modules

我正在使用Jersey w / Guice处理一些示例代码,它仍在进行中(我还没有开始修剪),但这里有一些摘录。

来自pom.xml:

<!-- [START Guice] -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
            <version>${guice.version}</version>
        </dependency>
<!-- [END Guice] -->
<!-- [START Jersey] -->
        <!-- Jersey -->
      <dependency>
          <groupId>org.glassfish.jersey.core</groupId>
          <artifactId>jersey-server</artifactId>
          <version>${jersey.version}</version>
      </dependency>
      <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet</artifactId>
          <version>${jersey.version}</version>
      </dependency>
      <dependency>
          <groupId>org.glassfish.jersey.ext</groupId>
          <artifactId>jersey-mvc-jsp</artifactId>
          <version>${jersey.version}</version>
      </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
      <dependency>
          <groupId>org.glassfish.hk2</groupId>
          <artifactId>guice-bridge</artifactId>
          <version>2.2.0</version>
      </dependency>
<!-- [END Jersey] -->
来自JerseyConfiguration.java的

public class JerseyConfiguration extends ResourceConfig {
    private final Logger log = Logger.getLogger(getClass().getName());

    @Inject
    public JerseyConfiguration(ServiceLocator serviceLocator, ServletContext servletContext) {
        log.info("Creating JerseyConfiguration");
        packages("com.example.gettingstarted");

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector((Injector) servletContext.getAttribute(Injector.class.getName()));
    }
}

来自GettingStartedServletContextListener:

public class GettingStartedServletContextListener extends
        GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new GettingStartedModule());
    }
}

来自GettingStartedModule.java:

public class GettingStartedModule extends ServletModule {

  @Override
  protected void configureServlets() {
      serve("/hi").with(HelloWorldServlet.class);
  }
}

来自HelloWorldServlet.java:

@SuppressWarnings("serial")
@Singleton
public class HelloWorldServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      resp.setContentType("text/plain");
      resp.getWriter().println("Hello, old friend");
  }
}