[ANN]: Jersey 2.0 w/ Guice

1,307 views
Skip to first unread message

Roger Kapsi

unread,
Aug 4, 2014, 12:25:39 PM8/4/14
to google...@googlegroups.com
We're using Guice at Squarespace and a different project we depend on is about to switch to Jersey 2.0. We've therefore taken a dive into HK2 and figured out why it didn't play nice with Guice (see JERSEY-2551). They fixed it in Jersey 2.11+ and the library is backwards compatible to Jersey 2.9.x.

  https://github.com/Squarespace/jersey2-guice

We hope it's useful for other Guice users and contributions and feedback are very welcome.

Thanks,
 Roger

John Wells

unread,
Aug 4, 2014, 3:05:36 PM8/4/14
to google...@googlegroups.com
Another really gr8 page about this is here:

https://github.com/mycom-int/jersey-guice-aop


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/2c6dadee-b2bd-4848-94fe-515656d74c4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gili

unread,
Aug 17, 2014, 6:40:22 AM8/17/14
to google...@googlegroups.com
Great idea. I haven't tested this myself but this sounds like it would work.

I hope to hear some validation from people actually using this library.

Gili

David Parish

unread,
Sep 17, 2014, 10:04:42 AM9/17/14
to google...@googlegroups.com
This really is terrific and very helpful.  Can you add a working sample webapp to help folks get started quickly?

Alexander Openkowski

unread,
Sep 17, 2014, 10:26:44 AM9/17/14
to google...@googlegroups.com
+1 for a working sample webapp, please!

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.

David Parish

unread,
Sep 17, 2014, 12:00:58 PM9/17/14
to google...@googlegroups.com
I think I'm pretty close.  Here's the GuiceConfig I'm using:

public class GuiceServletConfig extends JerseyGuiceServletContextListener {

    @Override
    protected List<? extends Module> modules() {
        AbstractModule module = new ServletModule() {


            @Override
            protected void configureServlets() {
                // bind the rest resource.
                bind(EngineResource.class);

                /* bind jackson converters for JAXB/JSON serialization */
                bind(MessageBodyReader.class).to(JacksonJaxbJsonProvider.class);
                bind(MessageBodyWriter.class).to(JacksonJaxbJsonProvider.class);

                // general use bindings.
                bind(EngineInvoker.class).to(GroovyEngine.class);
                bind(ResourceLoader.class).to(ActivititiResourceProvider.class);
                serve("/rest/*").with(JerseySingletonServlet.class);
            }

            @Provides
            @Singleton
            ProcessEngine getProcessEngine() {
                return ProcessEngines.getDefaultProcessEngine();
            }

        };

        return Lists.newArrayList(module);
    }
}


JerseySingletonServlet is just an extension of ServletContainer tagged as a Singleton. The problem is that the binding for the resource doesn't seem to be picked up. I get 404's for all the jersey paths.

Roger Kapsi

unread,
Sep 17, 2014, 2:18:21 PM9/17/14
to google...@googlegroups.com

Jeff Schnitzer

unread,
Jan 7, 2015, 5:43:53 AM1/7/15
to google...@googlegroups.com
If anyone is looking for something more plug-and-play, I've wrapped the Squarespace plugin in a dropwizard-like toolkit. Here's a stripped-down example GWizard service:

public class Main {
    @Path("/hello")
    public static class HelloResource {
        @GET
        public String hello() {
            return "hello, world";
        }
    }

    public static class MyModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(HelloResource.class);
        }
    }

    public static void main(String[] args) throws Exception {
        Guice.createInjector(new MyModule(), new JerseyModule()).getInstance(WebServer.class).startJoin();
    }
}

I personally think it's wiser to use the RESTEasy module, since it doesn't rely on manipulating private fields in Jersey, but if you are tied to Jersey this is probably the easiest option. The code and documentation are here:


Cheers,
Jeff
Reply all
Reply to author
Forward
0 new messages