Dropwizard tutorial


In this post, we will see about Dropwizard tutorial.

Dropwizard is java framework to develop high-performance restful web services faster. It will use some of the stable libraries to create simple,light-weight package so that you can actually focus on business logic rather than worrying about configuration.

Dropwizard provides out of the box support for configuration, application metrics, logging and many other operational tools.
It will help you to create production-ready web services in the shortest time possible.

Libraries used by Dropwizard

Jetty for HTTP

Dropwizard uses jetty as embed HTTP server for your projects.Dropwizard project will have main method which will start HTTP server and you can start your application seamlessly.

Jersey for rest

Dropwizard uses jersey library to create restful web application. This allows creating clean and testable classes which map HTTP requests to simple java objects.

Jackson for JSON

Jackson is well know library for mapping JSON data to java objects.Dropwizard uses Jackson for all JSON related conversions.

Other libraries

Metrics – library for providing JVM- and application-level metrics for monitoring
Guava – utility library
Logback and sl4j – logging library
Hibernate validator – Hibernate validator provides easy way to validate user input and generating helpful error messages.
Apache HttpClient and Jersey clients
JDBI Library to use relational databases.
Liquidbase – It is an open source database-independent library for tracking, managing and applying database schema changes.
Freemarker and mustache – simple templating system for UI
Joda time – Library for handling dates and time.
Let’s start with Dropwizard hello world example. In this tutorial, we will create simple Rest CRUD example.

1) Create a dynamic web project using maven in eclipse named “DropwizardHelloWordExample”

Maven dependencies

2) We need to add Dropwizard core dependency in the classpath.

Here is complete pom.xml

Create bean class

3) Create a bean name “Country.java” in org.arpit.java2blog.model.

Create Controller

4) Create a controller named “BookController.java” in package org.arpit.java2blog.controller.Please note that we will use JAXRS annotations over here.

Create Service class

5) Create a class BookService.java in package org.arpit.java2blog.service
It is just a helper class which should be replaced by database implementation. It is not very well written class, it is just used for demonstration.

Create Dropwizard main class

6) Let’s create DropwizardApplication.java. This is the class which we will run to start our application.When you run the application, it will automatically start embedded Jetty server.

The Application class takes care of various bundles and commands which provide basic functionality. You can use initialize method to configure or register health check metrics or object mappers etc.I will cover more about this in subsequent tutorials. Main method is entry point for our application as usual.

Environment class acts as a registry of all things our application can do.We have create new BookController and registered it with enviroment using “e.jersey().register(new BookController())”.

Dropwizard allows you to create as many resources (with different URI) as you want and register it with the environment.

7) It ‘s time to do maven build.

Right click on project -> Run as -> Maven build

Maven build in eclipse
8) Provide goals as clean install (given below) and click on run

Dropwizard maven build

Run the application

9) go to DropwizardApplication.java, right click -> Run as java application.

10) We will test this application in  postman , UI based client for testing restful web applications. It is chrome plugin. Launch postman.If you want java based client, then you can also use how to send get or post request in java.

Get method

11) Test your get method
URL :“localhost:8080/bookService/books”.

You will get following output:

get method

get books by id

URL :“localhost:8080/bookService/book/2”.

You will get the following output:

get method by id

Post method

12) Post method is used to create new resource. Here we are adding new Book “Effective Javascript” to book list, so you can see we have used new book json in post body.
URL: “localhost:8080/bookService/books”.

Put method

Use get method to check if above book has been added to book list.

After post method

Put Method

13) Put method is used to update the resource. Here will update pages of book “Hibernate in action” using put method.
We will update book json in body of request.
URL : “localhost:8080/bookService/books”

Put method

Use get method to check pages of  book “hibernate in action”.

After put method

Delete method

14) Delete method is used to delete resource.We will pass id of book which needs to be deleted as PathParam. We are going delete id:4 i.e. book “Learning Python” to demonstrate delete method.

URL : “localhost:8080/bookService/book/3”

Delete method

Use get method to check book list.

After delete method

As you can see, we have deleted country with id 3 i.e. Learning python.

Source code

Download source code – Dropwizard hello world

That’s all about Dropwizard tutorial.


Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.

No Responses

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog