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.
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.
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.
We will take a look at all the components that you will need to download and install.
Download Java 7 JDK from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html.
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.
Following the successful installation, set the
JAVA_HOMEenvironment variable with its value set to the path where you installed Java. In Windows, this may be something like
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.
PATHenvironment variable, adding the Maven directory suffixed with
C:\apache-maven-3.0.5\bin, so the
mvnexecutable will be available on all directories when using the command line or the terminal.
Download the MySQL Community Server installer for your operating system from http://dev.mysql.com/downloads/mysql/#downloads.
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.
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.