質問

I've got a fairly simple GWT app set up using GWT-RPC and Guice. At the moment I'm serving two things, the GWT-RPC service and an applet that accepts uploads. It looks something like this:

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

I'd like to be able to serve restlet resources or a restlet application from the same application and configure them in my guice module rather than the web.xml. Previously I've set up REST backed GWT apps using Restlet but it didn't use DI so I'm a bit lost as to how this should work.

eta: here's my 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>

update

This solves the problem, cheers!

    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

Finally went with a solution I adapted from http://hpehl.info/google-appengine-restlet-guice.html which allows me to using injection within my resources and a single line to bind in the guicemodule.

役に立ちましたか?

解決

Restlet offers a Restlet -> Servlet adapter with the org.restlet.ext.servlet.ServerServlet class. You should bind this to the desired path, and let it know the name of your Application like this:

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

Where com.you.MyApplication should be replaced with your actual implementation of the org.restlet.Application class.

他のヒント

Well, i tought you add your old web.xml. But this should solve your problem.

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

If you are not using plain Servlet as implementation of your REST api, it will be a bit tricky. Jersey can be integrated with Guice either.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top