How to Design RESTful Web Services with Dropwizard
By: Somnath Musib
Tags: Coding, Programming, Java, Software Engineering, Technology

Creating a new Dropwizard application

Now that, we have understood what Dropwizard is and few internal libraries, let us create a Dropwizard application to understand these concepts even further. Dropwizard dependencies are exposed as a maven dependency. In this section, we will develop a maven project and add the relevant dependencies.

Step 1: Creating a Maven Project

Open your favorite IDE and create a new maven project with archetype selected as maven-archetype-quickstart. Add the following dependency:

<dependency>

<groupId>io.dropwizard</groupId>

<artifactId>dropwizard-core</artifactId>

<version>2.0.0-rc9</version>

</dependency>

This single dependency ensures that all related components are downloaded and our application is ready to run.

Step 2: Creating a Dropwizard Configuration file

Each Dropwizard application has its own subclass of the Configuration class which specifies environment-specific parameters. These parameters are specified in a YAML configuration file which is deserialized to an instance of your application’s configuration class and validated. Create the following configuration file.

Custom Configuration file

Above mentioned configuration class extracts firstName and lastName parameters from the supplied YAML file. We will create a YAML file later stage of the application which will have following contents:

firstName: "John"

lastName: "Doe"

Step 3: Creating a Jersey Resource

Jersey resources are the meat-and-potatoes of a Dropwizard application. Each resource class is associated with a URI template. For our application, we need a resource which returns new Person instances from the URI /hello so our resource class looks like this:

Jersey resource

We are performing the following activities in the above class:

@Path annotation indicates that this class is a JAX-RS resource

@Produces indicates that this resource produces JSON data

@GET indicates that resource can be accessed over HTTP GET

@Timed let Dropwizard automatically records the duration and rate of invocations as a Metrics Timer

The method arguments indicate that if data is supplied in the request query parameters, then the same will be used. Otherwise, default values configured in the YAML file will be used

The response returned by the getPerson() method is a Person instance and will be mapped by Jackson. Following is the Person class representation:

Person class

Step 4: Creating a HealthCheck

Dropwizard strongly recommends providing health checks for a Dropwizard application. In fact, if health checks are not configured it warns the user at application startup. We have created the following health check for this application:

Dropwizard health check

Step 5: Creating an Application Class

Combined with Configuration subclass defined earlier, Dropwizard’s Application subclass forms the core of a Dropwizard application. The Application class pulls together the various bundles and commands which provide the basic functionality of the application. Following is the application class:

Dropwizard application class

Step 6: Building the executable jar

We are now done with our Dropwizard application development. We have created the barebones components and added a JAX-RS resource with an endpoint. Let us add the following maven shade plugin in order to build the executable JAR file:

Note the mainClass parameter in the configuration. It must the application main class.