0

我使用 GWT-RPC 和 Guice 设置了一个相当简单的 GWT 应用程序。目前我提供两件事,GWT-RPC 服务和一个接受上传的小程序。它看起来像这样:

public class MyGuiceModule extends ServletModule {
@Override
protected void configureServlets() {
        serve("/path/to/service").with(MyGWTServiceImpl.class);
        serve("/path/to/upload").with(MyUploadServlet.class);
        //bunch of bindings follow...
    }
}

我希望能够从同一应用程序中提供 restlet 资源或 restlet 应用程序,并在我的 guice 模块而不是 web.xml 中配置它们。以前我使用 Restlet 设置了支持 REST 的 GWT 应用程序,但它没有使用 DI,所以我对它应该如何工作有点迷茫。

eta:这是我的 web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">

  <web-app>

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

    <listener>
      <listener-class>com.whatever.MyGuiceBootstrap</listener-class>
    </listener>

    <!-- Default page to serve -->
    <welcome-file-list>
      <welcome-file>home.html</welcome-file>
    </welcome-file-list>

  </web-app>

更新

这样就解决了问题,加油!

    bind(ServerServlet.class).in(Singleton.class);
    Map<String,String> initParams = new HashMap<String,String>();
    initParams.put("org.restlet.application", "path.to.your.RestletApplication");
    serve("/api/*").with(ServerServlet.class,initParams);

更新2

最后,我采用了一个从http://hpehl.info/google-appengine-restlet-guice.html改编的解决方案,它允许我在我的资源中使用注入,并在 guicemodule 中绑定一行。

4

2 回答 2

1

Restlet 提供了一个带有org.restlet.ext.servlet.ServerServlet类的 Restlet -> Servlet 适配器。您应该将其绑定到所需的路径,并让它知道您的名称,Application如下所示:

serve("/api/*").with(ServerServlet.class);
getServletContext().setAttribute(
    "org.restlet.application", "com.you.MyApplication");

Wherecom.you.MyApplication应该替换为您的org.restlet.Application类的实际实现。

于 2013-10-04T09:27:47.173 回答
0

好吧,我坚持你添加你的旧 web.xml。但这应该可以解决您的问题。

serve("/api/*").with(ApiServlet.class);

如果您不使用普通的 Servlet 作为 REST api 的实现,那将有点棘手。Jersey 也可以与 Guice 集成。

于 2013-10-04T09:13:05.953 回答