2013-10-03 45 views
0

我有一個使用GWT-RPC和Guice設置的相當簡單的GWT應用程序。目前我服務於兩件事情,即GWT-RPC服務和一個接受上傳的applet。它看起來是這樣的:使用Restet與Guice DI&GWT

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,在我的吉斯模塊,而不是在web.xml配置。以前我使用Restlet設置了REST支持的GWT應用程序,但它沒有使用DI,所以我有點失落,應該如何工作。

埃塔:這裏是我的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); 

UPDATE2

最後一個解決辦法,我改編自http://hpehl.info/google-appengine-restlet-guice.html,讓我用我的資源和單行內注射在guicemodule綁定去了。

+0

你能否提供你的'web.xml'來解決你的問題。我無法想象你到底想要設置什麼。 –

+0

目前,web.xml只是加載一個guiceContextListener。然後,Guice委託給正確的servlet(如MyServletModule中配置),我將它添加到上面。我想要做的是使用相同的方法在路徑上添加一個rest框架(比如/ api)。我認爲這是可能的,但我寧願儘可能使用restlet。感謝您的期待! – tom

回答

1

Restlet提供了一個Restlet - > Servlet適配器,其類爲org.restlet.ext.servlet.ServerServlet。你應該結合本到指定路徑,讓它知道你Application像這樣的名字:

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

com.you.MyApplication應該與你的實際執行的org.restlet.Application類的來代替。

+0

布里爾,歡呼!看起來就是我以後的樣子。下一站將獲得DI爲資源類工作。 – tom

+2

是的,你也可以設置你的servlet。使用(ServerServlet),可以在'web.xml'中使用'',您可以創建'initParams = new HashMap '並將其傳遞到Servlet中,如'serve(「/ api/*」)。類,initParams);' –

+0

好的,所以我已經綁定了使用guice綁定(ServerServlet.class).in(Singleton.class);然而,我得到的404s而不是我想要的hello world :( – tom

0

嗯,我想你添加你的舊web.xml。但是這應該可以解決你的問題。

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

如果您沒有使用普通的Servlet作爲您的REST API的實現,那將會有點棘手。 Jersey也可以與Guice整合。

+0

這就是我最初要做的事情,就像那樣插入一個org.restlet.ext.servlet.ServerServlet。不幸的是,它只能通過web.xml或單獨的restlet.xml文件進行配置,我寧願避免這種情況。 http://restlet.org/learn/javadocs/snapshot/gae/ext/org/restlet/ext/servlet/ServerServlet.html – tom

+0

但是,您的評論促使我查看更多org.restlet.ext.servlet包深入的安我發現ServletAdapter,看起來它可以做我想做的事。所以歡呼! http://restlet.org/learn/javadocs/snapshot/gae/ext/org/restlet/ext/servlet/ServletAdapter.html – tom