Getting Started with Dropwizard - RESTful Web Services with Dropwizard (2014)

RESTful Web Services with Dropwizard (2014)

Chapter 1. Getting Started with Dropwizard

Dropwizard is an open source Java framework for the rapid development of RESTful Web Services putting together everything you'll need. You can have a production-ready application, making use of Jetty, Jersey, Jackson, JDBI, and Hibernate, as well as a large number of additional libraries that Dropwizard includes, either in its core or as modules. This solves the problem of manually adding, configuring, and wiring together lots of different libraries while building a web service application from scratch. Think of it like this: you will need Jersey to expose the web services, some other library for database interaction, and additional ones for validation and authentication, not to mention the overhead of dependency management, packaging, and distribution.

Throughout the chapters of this book, we are going to use Dropwizard and its components in order to build a sample application—that is, a phonebook application that exposes a set of RESTful Web Services that facilitate the storing and management of contacts. It works pretty much like your mobile phone's built-in phonebook application or any other contact management application.

Web service development with Dropwizard

We are going to use Jersey in order to build our web services. Jersey is the reference implementation of the JAX-RS standard (JSR 311), the Java API for RESTful Web Services. JAX-RS makes use of annotations, simplifying the development of web service applications.

The web services we'll build are going to produce JSON output. Dropwizard includes Jackson, which is a fast, configurable JSON processor, and is used by Jersey to transform plain Java objects to JSON representations.

Our application is going to use a database in order to store data. For our database interaction needs, we'll use JDBI. JDBI is a library that will allow us to easily create DAO interfaces. Data Access Objects would allow us to perform database operations by mapping Java methods to SQL queries and statements. JDBI comes as a Dropwizard module, allowing us to build Data Access Objects easily and fast.

Dropwizard includes validation, monitoring, and testing modules, which we'll use to ensure that our services will behave correctly in production environments. We are going to integrate Dropwizard's validation mechanisms, ensuring that each and every request to our web services is valid, before trying to serve it.

Preparing your development environment

Before we start creating Dropwizard applications, we need to set up our development environment, which will consist of, at least, Java (JDK 7), Maven, and MySQL.

Getting ready

Maven is a build manager for Java projects. We will use it to create and build our project. Our application's dependencies (on Dropwizard's modules) will be managed by Maven; we just need to add the appropriate entries in our project configuration file.

We need a database, so we will use MySQL for the needs of this book. MySQL is the most popular open source relational database management system—a common choice for web applications. Throughout the installation process, you will be prompted to create or configure the values of environment variables. This procedure varies from one operating system to another, and is something out of the scope of this book.

How to do it…

We will take a look at all the components that you will need to download and install.

Downloading and installing Java

1. Download Java 7 JDK from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html.

2. Since many installation packages are available, you need to select the appropriate one, depending on your operating system and platform.

3. After the download has completed, install the JDK by running the installer you downloaded, as shown in the following screenshot. There's no need to use settings different than the default ones for now. After a few steps, the installation will be completed.

Downloading and installing Java

4. Following the successful installation, set the JAVA_HOME environment variable with its value set to the path where you installed Java. In Windows, this may be something like C:\Program Files\Java\jdk1.7.0_40\.

Downloading and installing Maven

1. Maven installation is pretty straightforward. Just download Maven binaries from http://maven.apache.org/download.cgi and extract the contents of the package in a directory of your choice.

2. Modify the PATH environment variable, adding the Maven directory suffixed with \bin, like C:\apache-maven-3.0.5\bin, so the mvn executable will be available on all directories when using the command line or the terminal.

Downloading and installing MySQL

1. Download the MySQL Community Server installer for your operating system from http://dev.mysql.com/downloads/mysql/#downloads.

2. Run the installer and select to install MySQL. Keep the proposed, default installation settings.

3. At some point, you will be prompted to provide the MySQL Root Password. This is the password of the root user, which has full access rights. Enter a password of your choice, and proceed by clicking on the Next > button. The installation will be completed shortly.

Downloading and installing MySQL

4. Please choose a password that you will remember easily, as you will need to provide it at a later stage.

How it works…

We just completed the installation of the software packages required to build Dropwizard applications. We will use Maven to create the structure of our application, which will use MySQL as a persistent store for its data.

We are going to create a Maven project, and in its Project Object Model (POM) file, we will include the references (dependencies) to the Dropwizard components our application will use. Maven will automatically download and make them available for use throughout our project.