我使用 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 中绑定一行。