REST APIs using Dropwizard – Introductory Tutorial

Dropwizard is a popular java framework for restful service. It provides out of the box support for configuration, application metrics, logging and operational tools so the developers focus more on writing business logic and not on set ups.

Agenda

This tutorial should help you set up your dropwizard project and few APIs.

Let’s start!

Create Maven Project

Use your favourite IDE (Intellj or eclipse) and create a maven project. I will paste the screenshot for Intellij.

Click on Create New Project

Screen Shot 2018-07-12 at 3.25.03 PM

Select Maven in the left-hand side pan.

Screen Shot 2018-07-12 at 3.25.31 PM

Enter artifactid and groupid.

Read here to know about group id and artifact id.

Screen Shot 2018-07-12 at 3.35.22 PM

Enter the project name.

Screen Shot 2018-07-12 at 3.37.41 PM.png

Click Finish and you should see the project in your IDE. Add a file called dev.yml in your project. Your project structure should look like this.

Screen Shot 2018-07-12 at 5.30.23 PM
Project Structure

 

Add Dropwizard Dependency in your pom.xml

Add Configuration file

Under src/main/java, as shown in the figure; create a java file called myConfiguration.java which extends Configuration.java. This file is wired to your dev.yml. This helps in changing the configuration without changing the code.

Your dev.yml should look like this:

Essentially, this means MyConfiguration.getUrl() will give me the url defined in dev.yml.

Add Main Method called MyApplication

Your project structure should look like this now:

 

Screen Shot 2018-07-12 at 5.45.52 PM.png

You can now run your project with program argument server dev.yml in your run configuration. Your run configuration should look like this:

Screen Shot 2018-07-12 at 5.49.47 PM.png

If you run it, you should see the url printed in the logs.

Screen Shot 2018-07-12 at 5.50.50 PM.png

At this point, our dropwizard project is running. Now we will add get and post apis.

Add Resource File

Modify MyApplication.java to register your Resource.java

Your MyApplication.java should look like this:

Again run it. You should see the above-mentioned apis registered and printed in logs.

Screen Shot 2018-07-12 at 6.02.23 PM

Please notice /resource/getName and /resource/postName in the logs above.

Your apis are now ready to be used.

Congratulations.

Testing the apis

Please download postman

You can now test the apis. Screenshot of the get api being tested is pasted below:

Screen Shot 2018-07-12 at 6.07.11 PM.png

Follow the following tutorial to connect to database using hibernate.


Further Resource:

Dropwizard official documentation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: