New Dropwizard 1.0.5 Java Service
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.
Introduction
This assumes you have done a quick review of:
- http://www.dropwizard.io/1.0.5/docs/getting-started.html
- https://github.com/dropwizard/dropwizard
- Definitely worth checking out the google group - https://groups.google.com/forum/#!forum/dropwizard-user
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.
https://github.com/AnthonyHonstain/dropwizard-simple-skeleton
All you need to do is:
- Pull down the repo
git clone https://github.com/AnthonyHonstain/dropwizard-simple-skeleton.git
- 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 https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example
Alternatives - If you want to Archetype
Dropwizard provides an official mvn archetype https://github.com/dropwizard/dropwizard/tree/master/dropwizard-archetypes 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: http://www.dropwizard.io/1.0.5/docs/manual/testing.html
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.google.common.base.Optional;
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 javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import static org.junit.Assert.assertEquals;
public class IntegrationTest {
private static final String CONFIG_PATH = ResourceHelpers.resourceFilePath("hello-world.yml");
@ClassRule
public static final DropwizardAppRule<HelloWorldConfiguration> RULE =
new DropwizardAppRule<>(HelloWorldApplication.class, CONFIG_PATH);
private Client client;
@Before
public void setUp() {
client = ClientBuilder.newClient();
}
@After
public void tearDown() {
client.close();
}
@Test
public void testHelloWorld() {
final Optional<String> name = Optional.of("TestName");
final Saying saying = client.target("http://localhost:" + RULE.getLocalPort() + "/hello-world")
.queryParam("name", name.get())
.request()
.get(Saying.class);
assertEquals("Hello, " + name.get() + "!", saying.getContent());
}
}
Which should run automatically when you build mvn package
or mvn test
Results
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