Dropwizard Can Be Simple

We had a conversation at work the other day about Dropwizard vs Spark for Java services. One of my coworkers liked the simplicity of Spark, especially when building example applications. I pointed out that Dropwizard can also be pretty simple, but their Getting Started guide makes it hard to realize it. It walks through a lot of steps that are important for a production service, but in my opinion, should not be part of the initial Getting Started guide. So I decided to show what a simple Dropwizard app looks like without all of that stuff.

My example has no config class, no config file, no healthcheck, no representation classes, etc. But it would be easy to add this stuff later as necessary instead of starting with everything up front.

The entire app is only two Java files and a pom.xml. The Java files are split into the application (with the main method) and the resource (i.e. the routes). Here is the bare bones application:

public class DropwizardExampleApplication extends Application<Configuration> {  
  public static void main(String[] args) throws Exception {
    new DropwizardExampleApplication().run(args);
  }

  @Override
  public void run(Configuration configuration, Environment environment) {
    environment.jersey().register(new Resource());
  }
}

As you can see, all you really need is a main method that calls run and line to register the resource. The preferred way of running Dropwizard is via packaged jars:

% mvn package && java -jar target/dropwizard-example-1.0-SNAPSHOT.jar server

You can also run the main method directly from an IDE for easy iteration.

Resources can also be super simple. For example, here is what a simple GET route looks like:

@Path("/")
public class Resource {  
  @GET
  @Path("/hello")
  public String hello() {
    return "Hello";
  }
}
% curl localhost:8080/hello
Hello  

Methods are annotated with @Path and Dropwizard (Jersey) does the rest. If you want query params, you just annotate the method argument:

  @GET
  @Path("/query")
  public String query(@QueryParam("message") String message) {
    return "You passed " + message;
  }
% curl 'localhost:8080/query?message=hello'
You passed hello  

A different annotation gives you form params:

  @POST
  @Path("/postparam")
  public String postParam(@FormParam("message") String message) {
    return "You posted " + message;
  }
% curl -X POST -d 'message=hello' localhost:8080/postparam
You posted hello  

For general POST bodies, you don't even need an annotation:

  @POST
  @Path("/postbody")
  public String postBody(String message) {
    return "You posted " + message;
  }
% curl -X POST -d 'hello' localhost:8080/postbody
You posted hello  

For more complicated POSTs, you would probably want to use a representation class. But for a simple example, I think Strings are pretty easy.

All in all, it's not quite as simple as Spark, but I think it's pretty close.

The entire project is on GitHub: https://github.com/pgr0ss/dropwizard-example