Preface - RESTful Web Services with Dropwizard (2014)

RESTful Web Services with Dropwizard (2014)

Preface

Dropwizard is a Java development framework for RESTful Web Services. It was initially built by Yammer to be used as the base of their backend systems. Dropwizard is production-ready; it encapsulates everything you will need for RESTful development.

Jersey, Jackson, jDBI, and Hibernate are only some of the libraries bundled with Dropwizard. Applications built on Dropwizard run on an embedded Jetty server—you don't need to worry where to deploy your application or whether it is compatible with your target container.

Using Dropwizard, you will be able to build a fast, secure, and scalable web service application efficiently with minimum effort and time.

Dropwizard is open source, and all of its modules are available though Maven repositories. That way, you are able to integrate every library you wish—if it's not already present—just by adding the appropriate dependency entry on your pom.xml file. Basic knowledge and understanding of Maven is required.

What this book covers

Chapter 1, Getting Started with Dropwizard, will guide you through the basics of Dropwizard, helping you to get familiar with its concepts and also prepare your development environment.

Chapter 2, Creating a Dropwizard Application, will introduce Maven and how to use it to create a Dropwizard application. This covers generating the structure of an empty application, based on the default artifact, and the necessary modifications required in order to start building a Dropwizard application.

Chapter 3, Configuring the Application, presents the methods available to externalize your application's configuration by enabling the use of a configuration file along with a configuration class that is tasked with fetching, validating, and making the configuration values available throughout the application.

Chapter 4, Creating and Adding REST Resources, will guide you through the implementations of your application's most important aspect: the resource class. You will learn how to map URI paths and HTTP verbs to methods of the resource class and how to add new resources to a Dropwizard application.

Chapter 5, Representations – RESTful Entities, deals with the modeling of representations to actual Java classes and how the POJOs are automatically transformed to JSON representations by Jackson.

Chapter 6, Using a Database, demonstrates the integration and usage of jDBI, how to create data access objects from interfaces, and using jDBI's SQL Object API in order to interact with the database. The additional configuration modifications needed are also presented in this chapter.

Chapter 7, Validating Web Service Requests, presents the usage of Hibernate Validator in order to validate requests from a web service client prior to fulfilling them.

Chapter 8, The Web Service Client, demonstrates how to create a managed Jersey HTTP client to be used by a Dropwizard application in order to interact with web services through WebResource objects.

Chapter 9, Authentication, goes through the basics of web service authentication and guides you through the implementation of a basic HTTP authenticator and how to adapt it to the resource class as well as the HTTP client of your application.

Chapter 10, The User Interface – Views, shows the usage of the Dropwizard views bundle and the Mustache template engine in order to create an HTML interface for the web service client.

Appendix A, Testing a Dropwizard Application, demonstrates the usage of Dropwizard's testing module for the creation of automated integration tests. This appendix also deals with the implementation of runtime tests for our application, which are known as health checks. You will be guided through the implementation of a health check that ensures that your HTTP client can indeed interact with a web service.

Appendix B, Deploying a Dropwizard Application, explains the necessary steps you need to take in order to deploy a Dropwizard application to a web server by using a separate configuration file and securing the access to you application's admin port.

What you need for this book

In order to follow the examples and the code snippets presented throughout the book, you will need a computer with a Linux, Windows, or OS X operating system. A modern Java code editor/ IDE such as Eclipse, Netbeans, or IDEA is really going to help you. You will also need Version 7 of Java Development Kit (JDK) as well as Maven and MySQL server. Additional dependencies will be fetched by Maven, so you will need a working Internet connection.

Who this book is for

This book's target audience is software engineers and web developers that have at least basic Java knowledge and a basic understanding of RESTful Web Services. Knowledge of SQL/MySQL usage and command-line scripting may also be needed.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Add a new method in the Contact class named #isValidPerson()."

A block of code is set as follows:

import java.util.Set;

import javax.validation.ConstraintViolation;

import javax.util.ArrayList;

import javax.validation.Validator;

import javax.ws.rs.core.Response.Status;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

private final ContactDAO contactDao; private final Validator validator;

public ContactResource(DBI jdbi, Validator validator) {

contactDao = jdbi.onDemand(ContactDAO.class); this.validator = validator;

}

Any command-line input or output is written as follows:

$> java -jar target/app.jar server conf.yaml

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "At some point, you will be prompted to provide the MySQL Root Password."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.