Skip to content

GWizard, like Dropwizard but Guicier

Jeff Schnitzer edited this page Mar 12, 2017 · 1 revision

January 6, 2015

If you've been using Guice for a while, you probably can't imagine how you could ever again live without dependency injection. At this point I pretty much "think in DI". So it was with some trepidation that I started a client project using Dropwizard.

Dropwizard is a fantastic idea - make some opinionated decisions (ones which I for the most part agree with) and cut out most of the boilerplate of web applications. Unfortunately there's one critical opinion missing - DI - and consequently there's still more boilerplate than I can stand. Furthermore, the heavy reliance on Jersey for AOP really complicates testing (more in the next post) - you can't, for example, access a database without starting up the whole web stack!

After progressively replacing piece after piece of Dropwizard with Guice-friendly alternatives, I finally took the final plunge and built a Dropwizard-like framework, based on Guice. GWizard is the result.

The first thing to note is that this is a minimal framework - there are more lines of documentation than lines of code. However, it squarely hits the pain points of building a JAX-RS application with Guice. Here is a complete REST service using GWizard:

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.voodoodyne.gwizard.rest.RestModule;
import com.voodoodyne.gwizard.web.WebServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

public class Main {
    /** A standard JAX-RS resource class */
    @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 RestModule())
            .getInstance(Run.class)
                .start();
    }
}

This is about the amount of boilerplate that I can stand. It could be better; using Reflections to auto-discover @Path-annotated resources would help. But I'm pretty ok with this.

GWizard does more; check out the example application. You'll find easy Dropwizard-style configuration files, JPA integration, logging, and an example of how to write typesafe tests against your JAX-RS api without mocking and stubbing!

Check it out: https://github.com/gwizard/gwizard

original