New Dropwizard 1.0.5 Java Service

Dropwizard Dec 10, 2016

Dropwizard is a great tool for creating RESTful Java services. Are you interested in a walk-through of standing up a fresh Dropwizard service? Here are some resources you might find helpful, and the example I created while going through the process.

What you should get from this post?

  • A basic Dropwizard service.
  • Build a shaded jar with maven.
  • An initial integration test.


This assumes you have done a quick review of:

The official Dropwizard getting-started project has gotten significantly better as the project has matured.

I did all of this on:

  • Ubuntu 16.04.1 LTS Xenial
  • openjdk version "1.8.0_91"
  • Apache Maven 3.3.9

Getting Started

The part that troubled me the most in getting a dropwizard service off the ground was getting maven or gradel setup so that I could get a build running. On many of the production systems I worked with, the maven choices were long set in stone. So I rarely got to experience standing one up from scratch. Your experience may differ.

I created a VERY bare bones example, it is basically just the result of what you get from going the official getting-started.html project.
All you need to do is:

  • Pull down the repo git clone
  • Build the project mvn package
  • Start the jar java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world.yml

Boom! You have should have a java service running locally.

Alternatives - Official Sample Service

The dropwizard project now provides an official example project that has some great references

Alternatives - If you want to Archetype

Dropwizard provides an official mvn archetype but in general I would prefer to interact with a basic sample/skeleton project (I do not personally enjoy constructing or updating archetype projects - the cost of creating+maintaining doesn't usually make sense for me.

Everyone Needs Tests

I love automated testing, hence I refuse to move forward without immediately having some reasonable scaffolding in place for unit and integration tests.

The official documentation gives you a good overview of your options:

I went ahead with a pretty simple integration test, the confusing part for me was making sure the config file was in the right place for testing (I needed to create a hellow-world.yml config under src/test/resources).

package com.example.helloworld;


import com.example.helloworld.core.Saying;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit.DropwizardAppRule;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;


import static org.junit.Assert.assertEquals;

public class IntegrationTest {

    private static final String CONFIG_PATH = ResourceHelpers.resourceFilePath("hello-world.yml");

    public static final DropwizardAppRule<HelloWorldConfiguration> RULE =
            new DropwizardAppRule<>(HelloWorldApplication.class, CONFIG_PATH);

    private Client client;

    public void setUp() {
        client = ClientBuilder.newClient();

    public void tearDown() {

    public void testHelloWorld() {
        final Optional<String> name = Optional.of("TestName");
        final Saying saying ="http://localhost:" + RULE.getLocalPort() + "/hello-world")
                .queryParam("name", name.get())
        assertEquals("Hello, " + name.get() + "!", saying.getContent());

Which should run automatically when you build mvn package or mvn test


Now you should have a basic Dropwizard service with tests that can respond to requests at http://localhost:8080/hello-world

Cheat sheet:

# Clean up past work.
mvn clean

# Create a new shaded jar and run tests.
mvn package

# Start the dropwizard service.
java -jar target/dropwizard-sample-0.0.1-SNAPSHOT.jar server hello-world.yml

build example