Getting Started with Dropwizard using java

View: 1136    Dowload: 0   Comment: 0   Post by: hanhga   Category: Javascript   Fields: Other

Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch. This article is going to cover the very basics. The goal is to implement and run a Dropwizard application which is able to respond to an HTTP GET request.

After a short introduction to Dropwizard and our application domain, we will see how to create a new project, talk about its structure, and configure it. Later on we will implement a simple RESTful API before finally building and running your application.

The sample project is available on GitHub. Key commits are tagged and I’ll be referencing them throughout this post after each major change. If you feel that something is unclear and wish to see a snapshot of the whole project, feel free to check out the referenced tag.

The only prerequisites you need to have installed are Maven, JDK, and a text editor or IDE of your choice.

What is Dropwizard?

As already stated, Dropwizard is a framework for building RESTful web services in Java. In essence, it is a glue framework which bundles together popular and battle-tested Java libraries and frameworks to make it easier to start building new RESTful web services.

Here’s an incomplete list of some of the libraries that Dropwizard uses.

  • Jersey – reference implementation of JAX-RS, the API for RESTful web services in the Java platform
  • Jackson – library for processing JSON data
  • Jetty – HTTP server and Java Servlet container
  • Metrics – library for capturing JVM- and application-level metrics for monitoring
  • Guava – utility library
  • Logback – logging library

Introducing Our Demo Application Domain

To get a better understanding of Dropwizard, we’re going to build a RESTful web service from the ground up. It is going to be a back-end for a hypothetical events app. Imagine you’ve moved to a new city and would like to plan your evening. This app will list events based on your search criteria and location. The back-end should be able to provide a list of events. It should also be possible to add new events and update existing events. To keep things simple, we’re going to exclude user authentication and authorization for now.

Project Structure

I’m going to use Maven as a build tool. In my opinion, the easiest way to get started with a new application is to create a project using the dropwizard-archetype. Archetype is essentially a tool for bootstrapping a Maven project from a template.

Run the following command in your terminal to get started with a new Dropwizard project.

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

As of October 2016, the latest version of Dropwizard is 1.0.2. Be sure to check the most recent version and updatearchetypeVersion accordingly. Running the archetype:generate command prompts you to enter some project specific fields (e.g. groupId, artifactId). When entering the project name, keep in mind that this field is used to generate application and configuration classes. Use CamelCase and do not insert spaces to make sure that the generated class names are going to be valid.

For example, I named the application Events. Therefore the generated classes are going to be named EventsApplicationand EventsConfiguration. If I had used events-app as the name, event-appApplication and event-appConfiguration would have been generated. Unfortunately, these are invalid class names and the compiler will point that out to you.

On successful completion, This is what the generated folder structure looks like.

├── config.yml
├── pom.xml
├── README.md
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── mydomain
    │   │           ├── api
    │   │           ├── cli
    │   │           ├── client
    │   │           ├── core
    │   │           ├── db
    │   │           ├── EventsApplication.java
    │   │           ├── EventsConfiguration.java
    │   │           ├── health
    │   │           └── resources
    │   └── resources
    │       ├── assets
    │       └── banner.txt
    └── test
        ├── java
        │   └── com
        │       └── mydomain
        │           ├── api
        │           ├── client
        │           ├── core
        │           ├── db
        │           └── resources
        └── resources
            └── fixtures

Application configuration is stored in config.yml. We’re going to look at it in more detail in the next paragraph. Most of the meat is located in src/main/java including the two aforementioned classes – EventsApplication andEventsConfiguration.

The following is an overview of which files should go into which folder:

  • api – Here live your application representations, which are essentially the entities of your API. They are serialized and deserialized to JSON.
  • cli – Contains commands that can be executed from the command line. They are not covered in this tutorial.
  • client – HTTP client implementation of your API. For easy consumption of your API you can implement a client and place it here. We’re not going to do it in this tutorial.
  • core – This is where you implement your domain logic.
  • db – Classes related to database access.
  • health – Contains health checks. These are runtime tests which verify whether your application is behaving correctly.
  • resources – Jersey resources.

We’re not going to use all of these packages since I’d like to keep the tutorial on the easier side. Dropwizard isn’t strict about the package naming scheme. If you wish you can rename and restructure the package hierarchy to suit your needs better.

The initial project structure can be seen by checking out the starting-point tag.

  rootPath: /api/*
dateFormat: yyyy-MM-dd'T'HH:mmZ

Of course we need to access the custom configuration parameter dateFormat from within the running application. When Dropwizard is started, configuration fields are mapped to a configuration class’ member fields.

So let’s edit our configuration class and include the dateFormat field. Open EventsConfiguration and add the following lines:

private String dateFormat;

public String getDateFormat() {
    return dateFormat;

An application shouldn’t trust an external configuration file to be correct. To help validate its correctness, we can use Hibernate Validator. The @NotEmpty annotation makes sure the string is not empty. Otherwise Dropwizard fails to start.

Keep in mind that this does not check if the date format pattern is actually valid. It only checks if it is defined in the configuration file. You could create your own custom validator which will print out a nice error message at start-up if the pattern is invalid. To keep things simpler, I’m not going to tackle this issue at the moment.

Check out app-configuration tag to see the project after configuration changes.

Application Class

This is what the generated application class looks like.

public class EventsApplication extends Application<EventsConfiguration> {
    public static void main(final String[] args) throws Exception {
        new EventsApplication().run(args);

    public void run(EventsConfiguration configuration, Environment environment) {

    public void initialize(Bootstrap<EventsConfiguration> bootstrap) {

When starting the service, the main method inside the EventsApplication class gets invoked. This is the entry point for a Dropwizard application. A new EventsApplication object is created and its run method is called. The run method is used to configure Dropwizard’s environment.

This is the place to apply the date pattern used in our application. For that we configure Jackson’s ObjectMapper, which will later (de)serialize the request and response parameters from Java classes to JSON and vice versa. Let’s modify its date format by explicitly creating a new instance of SimpleDateFormat and passing it the pattern from the configuration object.

public void run(EventsConfiguration configuration, Environment environment) {
    DateFormat eventDateFormat = new SimpleDateFormat(configuration.getDateFormat())

The application class has one more method we need to look at although we’re not going to add any code inside it:initialize is used to go through the application’s bootstrapping phase. Here you can add bundles and commands for example. Bundles are groups of reusable functionality – think of them as add-ons to your application. Commands can be used to create additional functionality that can be accessed from the command line.

Checkout date-format-configuration tag to see the project after changes made in the application class.

Creating a Representation of Domain Objects

Representations are the entities of your API. They represent the current or the desired state of a resource. For example, when a client makes an HTTP GET request to the events resource, the server sends back a representation of the current state of events. Representations can be serialized to any format but we’re going to use JSON in this tutorial.

Let’s think about what fields we want to include in the events representation class. An event should have a name. Also, we need to know when and where the event is taking place. Hosts might want to add additional information about the event. This calls for a description field. It is a good idea to include an id as well since we want to make sure each event is uniquely identifiable.

Create a new Event class in com.mycompany.api (or whatever package naming scheme you are using).

public class Event {
    private long id;
    private String name;
    private String description;
    private String location;
    private Date date;

    //getters and setters omitted

In most cases using a string to represent a location might not be enough. You could create a Location class which can hold more detailed information (e.g. latitude, longitude). Dropwizard will make sure nested objects are serialized correctly.

Check out event-representation tag to see the project after creating a new representation class.

Create a Simple Resource

Under the hood, Dropwizard uses Jersey to build RESTful web services. Jersey is a reference implementation of JAX-RS(JSR 311 & JSR 339). A lot goes on in implementing resources and I don’t intend for this article to be a comprehensive overview of Jersey. If you wish to learn more, feel free to browse Jersey’s documentation.

To get started with Jersey, let’s create a resource for events. Additionally, we need to define a URL which can be used to access the resource and retrieve its current state.

Let’s create a very simple implementation of the events resource. The goal is to expose a URL for retrieving all events. For now we’re going to return dummy data. Create a new class called EventResource in the resources package.

public class EventResource {

    public List<Event> allEvents() {
        Event e = new Event();
        e.setDate(new Date());
        e.setDescription("Please be on time!");
        e.setLocation("221B Baker Street");

        return Collections.singletonList(e);

The @Path annotation specifies the URL root for this resource. In the next line we specify the representation format. Representation classes are serialized to JSON and returned to the client. EventResource at its current state has only one method: allEvents. It is annotated with the HTTP verb it should respond to. So for example, when a GET request to/eventsis received by Dropwizard, allEvents gets invoked. In REST speak, GET is used to retrieve a representation of the current state of a resource.

Inside the method a new Event object is instantiated with dummy data. The event object is wrapped in a list which is then returned. I’m using singletonList from the Collections class for convenience. Creating a new list manually and adding the element to the list would work perfectly as well.

To see the project after resource creation, check out event-resource tag.

Registering a Resource

By default Dropwizard doesn’t know about resource classes and we need to tell it explicitly which resources to expose. Technically speaking, resources need to be registered with Dropwizard’s Jersey Environment. As mentioned earlier, resource registration can be done in the EventsApplication class’ run method.

Modify it to look like the following example.

public void run(EventsConfiguration configuration, Environment environment) {
    DateFormat eventDateFormat = new SimpleDateFormat(configuration.getDateFormat())

    EventResource eventResource = new EventResource();

The current state of the project can be seen by checking out register-resource tag.

Building the Application

To run a Dropwizard application, it needs to be built first. It is recommended that you build your application as a fat JAR file. This is a JAR file that contains the classes from your project and all the libraries it depends on, so everything the application needs is bundled into a single file, which makes it easy to deploy and run. The same artifact can be promoted from test to staging to production while being sure there’s no version changes in libraries used.

If you followed the article and created a new project with dropwizard-archetype then Maven is already configured to build a fat JAR file using the maven-shade-plugin.

Simply run the following command in the root of your project (the folder where pom.xml is located).

mvn package

Running the Application

You can now execute the jar file as follows.

java -jar target/events-0.0.1-SNAPSHOT.jar server config.yml

For successful execution, we need to pass two command line arguments to Dropwizard. The first one, server, instructs Dropwizard to start an HTTP server and run the service. The second argument is a configuration file which is required by the server command.

If the app started without any errors, point your browser (or a command line http tool such as curl) tohttp://localhost:8080/api/events. The server should return with a valid response containing the following message body.

[{"id":1,"name":"Birthday","description":"Please be on time!","location":"221B Baker Street","date":"2016-10-03T12:09+0200"}]


By now you should have a basic idea what Dropwizard is about. With the help of existing libraries, Dropwizard enables developers to easily create new RESTful web services.

There’s a lot we did not cover. First of all, the requirements we set for our application are not met. At the moment, only dummy data is returned to the client. There’s going to be a follow-up article where we’re going to add additional features to the Events service and look into parts of Dropwizard we were not able to cover in this article.

Getting Started with Dropwizard using java

Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

Posted on 15-10-2016 


To comment you must be logged in members.

Files with category

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 237    Download: 3   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • AngularJS and REST API

    AngularJS and REST API

    View: 369    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

  • Collective Intelligence, Recommending Items Based on Similar Users' Taste

    Collective Intelligence, Recommending Items Based on Similar Users' Taste

    View: 301    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

  • Think Like a Bird for Better Parallel Programming

    Think Like a Bird for Better Parallel Programming

    View: 278    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

  • Getting Started with the Bing Search APIs

    Getting Started with the Bing Search APIs

    View: 302    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

  • Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    View: 676    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Continuous in-line request/result batching, real-time stream sending/processing, asynchronous data transferring and parallel computation for best performance and scalability

  • Iteration Over Java Collections with High Performance

    Iteration Over Java Collections with High Performance

    View: 279    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Java developers usually deal with Collections such as ArrayList, HashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in...

  • SR2JLIB - A Symbolic Regression Library for Java

    SR2JLIB - A Symbolic Regression Library for Java

    View: 282    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Grammar-Guided Genetic Programming library featuring: multi-threading, just-in-time compilation of individuals, dynamic class loading, and JNI interfacing with C/C++ code

File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd