RESTful Web Services with Dropwizard

4.5 (2 reviews total)
By Alexandros Dallas
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

The RESTful Web Services concept is constantly growing compared to SOAP technologies. Used by many API providers, RESTful has started to become the architecture of choice for the enterprise SOA as well. Dropwizard combines everything needed to create production ready RESTful Web Services, combining the most powerful, stable and well-known RESTful Java-based libraries.

This practical guide will help you learn how to create, configure, and implement a dynamic Web Service application by showcasing practical examples using the most important modules of Dropwizard and combining them together.

Starting from using Maven to create an empty default Java application to deploying a production-ready scalable Web Service application, this book demonstrates the capabilities of Dropwizard and the advantages of using it.

Discover how to use Jersey to create rest resources, and add them to Dropwizard's bootstrapped environment so they can be served by its embedded Jetty server. Learn how to combine and utilize the Database. Understand more about Configuration, Testing and Monitoring libraries to create a Web Service application in a step-by-step manner using solid examples. Also gain insight on how to secure your Web Services and validate incoming requests with minimum effort.

This book is an invaluable asset to anyone who wants to create a RESTful Web Service application with the combined libraries of Dropwizard.

Publication date:
February 2014
Publisher
Packt
Pages
112
ISBN
9781783289530

 

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.

  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.

  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.

About the Author

  • Alexandros Dallas

    Alexandros Dallas studied Applied Informatics in Management and Economy and is now a software test engineer based in Athens. He has a solid programming/software development background, and whenever he is free, he spends his time contributing to open source projects. He is well aware of Dropwizard's core libraries, such as Jersey, since his interests include the development and integration of web APIs.

    Browse publications by this author

Latest Reviews

(2 reviews total)
Thorough, good quality, good price
Excellent
Book Title
Access this book, plus 7,500 other titles for FREE
Access now