RESTful APIs using DropWizard FrameWork in Java
By: Scanner Java
Tags: Dropwizard, Java, Rest Api

There are numerous articles on internet illustrating about springboot compared to dropwizard. In this blog, I would be showing how to kickstart a dropwizard application up and running.

So, no bragging, let’s get right into it.

prerequisites

Intellij IDE Community Edition (Best IDE for Java Development) Java SDKs Apache Maven (a build tool) Postman (optional) Prior knowledge of executing java programs

1. Create Dropwizard Application template

In the terminal, run the following command to initialise the DW application

mvn archetype:generate -DarchetypeGroupId=io.dropwizard.archetypes -DarchetypeArtifactId=java-simple -DarchetypeVersion=2.0.17

We will have to provide relevant details in setting up the application for us by maven in the terminal.

Define value for property ‘groupId’: com.example

Define value for property ‘artifactId’: dropwizard-app

Define value for property ‘version’ 1.0-SNAPSHOT: :

Define value for property ‘package’ com.example: :

[INFO] Using property: description = null

Define value for property ‘name’: HelloWorld

[INFO] Using property: shaded = true

Confirm properties configuration:

groupId: com.example

artifactId: dropwizard-app

version: 1.0-SNAPSHOT

package: com.example

description: null

name: HelloWorld

shaded: true

In the above details groupId, artifactId and version identifies your application, And name describes your Main class. We can skip the package field if you dont know about it. For version, better go with the default one.

Now, your DropWizard Application template is ready

2. Add Application configurations in config.yml

In general config.yml is populated with uninterrupted data, such as, localised strings, default values, server specifications, database credentials, etc. For the scope of the blog, we can confine to having a default templates and names. Just add the following lines in config.yml . Primary reason for having these in config file rather than in code is loose coupling of components.

3. Accessing fields from config.yml

To access the fields we defined in config.yml we have to have the attributes, getters and setters for the same in src/main/java/HelloWorldConfiguration java class as shown

@NonEmpty annotation makes sure the strings are not empty.

Also add getters and setters for the aforementioned fields like shown

@JsonProperty is annotation used by Jackson library to deserialise the config.yml into attributes.

Note: Make sure all the methods and variable names follow camelcase style.

4. Adding DTOs

Most of the times payloads in the http communication are JSONs. But in java everything is an object. How do you convert this object into JSON? Jackson library does the most hard work for you. We just have to create the DTOs, stands for Data Transfer object, which are directly mapped to JSON, where attributes identifiers are keys and values being values.

{

greeting: "Hello, Scanner"

message: "Your name contains 2 vowels."

}

Create a java class file src/main/com.example/api/Response.java as shown

The response above mentioned is a simple one, but we can also have a nested responses as well.

5. Adding API endpoints

Create a java class src/main/com.example/resources/HelloWorldResource.java

We just created http://localhost:8080/helloworld/vowels/Warner. Now we add an API endpoint to our application, Next we have to register this resource with the application.

6. Registering resources

To register the HelloWorldResource do the following inside the run() method in HelloWorldApplication as following

7. Building the application

In order to build the application using maven, we need to point where our entry point is residing. Mostly commonly used java application builds are JAR files known as Java Archives. Add the following in <build><plugins> section.

Once you’ve added this reload the pom.xml to download the dependencies. Open the terminal in the IntelliJ IDE, then execute the following command to build the application.

mvn clean install

This will build your application inside target folder as dropwizard-app-1.0-SNAPSHOT.jar

8. Run the application

Finally run the application along with arguments as following

java -jar target/dropwizard-app.1.0-SNAPSHOT.jar server config.yml

Now to check the responses go to the link, and try to skip the name to see the default message. To best visualise and have more control over requests, please use postman tool. URL: http://localhost:8080/helloworld/vowels/Warner