6

我在我的 GAE Java 应用程序中遇到了一个难题:我正在尝试编写一个将由 GAE 后端处理的 servlet,它还必须使用Guice 依赖注入来启动所有 servlet 的字段。我尝试了多种方法,但似乎没有一个对我有用。

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

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

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

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

最后一种可行的方法是在 中配置 servlet-mapping web.xml。传统的方法。这最终允许我的后端检测 URI 映射(这与Google 的 Java GAE 后端文档中所写的内容一起出现:“后端与主应用程序版本共享 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文件配置为 have <public>true</public>,但我正在通过从 GAE 应用程序的前端查询后端来测试这些 HTTP 请求调用。另外,请注意,我的前端实例正在使用BackendService此处为 javadoc)正确检索后端地址。

我之前问过的其他相关 Stackoverflow 问题

4

1 回答 1

1

后端已被弃用,取而代之的是Modules

我正在使用带有 Guice 的Jersey编写一些示例代码,它仍在进行中(我还没有开始修剪东西),但这里有一些摘录。

来自 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");
  }
}
于 2014-08-22T23:52:53.297 回答