Book Image

Building a RESTful Web Service with Spring

By : Ludovic Dewailly
Book Image

Building a RESTful Web Service with Spring

By: Ludovic Dewailly

Overview of this book

Table of Contents (17 chapters)
Building a RESTful Web Service with Spring
About the Author
About the Reviewers


In today's connected world, APIs have taken a central role on the Web. They provide the fabric on which systems interact with each other. And REST has become synonymous with APIs. REpresentational State Transfer, or REST, is an architectural style that lends itself well to tackling the challenges of building scalable web services.

In the Java ecosystem, the Spring Framework is the application framework of choice. It provides a comprehensive programming and configuration model that takes away the "plumbing" of enterprise applications.

It will, therefore, come as no surprise that Spring provides an ideal framework for building RESTful web services. In this book, we will take a hands-on look at how to build an enterprise-grade RESTful web service with the Spring Framework. As an underlying theme, we will illustrate the concepts in each chapter with the implementation of a sample web service that deals with managing rooms in a hotel.

By the end of this book, readers will be equipped with the necessary techniques to create a RESTful web service and sufficient knowledge to scale and secure their web service to meet production readiness requirements.

What this book covers

Chapter 1, A Few Basics, discusses the REST architecture approach and its underlying principles.

Chapter 2, Let's Get Started, enables us to put the scaffolding together, before building a RESTful web service.

Chapter 3, Building RESTful Web Services with Maven and Gradle, looks at the building blocks of creating RESTful endpoints.

Chapter 4, Data Representation, discusses how to manage data representation before we proceed further with building more endpoints. This chapter also offers advice on creating common response formats and error handling.

Chapter 5, CRUD Operations in REST, expands on the previous chapters and takes a look at how you can map CRUD operations to RESTful endpoints.

Chapter 6, Performance, explains that for a web service to be production-ready, it needs to be performant. This chapter discusses performance optimization techniques.

Chapter 7, Dealing with Security, looks at how to ensure a web service is secure by delving into steps that designers need to take. This chapter looks at how to deal with authentication and authorization, as well as input validation techniques.

Chapter 8, Testing Restful Web Services, looks at how to guarantee that a web service delivers the expected functionality, and the testing strategies that designers need to consider. This chapter offers readers the approaches for creating comprehensive test plans.

Chapter 9, Building a REST Client, tells us how for a web service to be of any use, it must be consumed. This penultimate chapter focuses on how to build a client for RESTful web services.

Chapter 10, Scaling a Restful Web Service, explains that scalability is a vast topic and encompasses many aspects. In this last chapter, we discuss what API designers can put in place to help the scaling of their service.

What you need for this book

Readers will need the version 8 of the Java Development Kit (JDK) and Apache Maven to build the code samples in this book. In addition, readers who wish to delve into creating their own service, or simply look at the code samples in more detail, should equip themselves with their preferred IDE.

Who this book is for

This book is intended for those who want to learn to create RESTful web services with the Spring Framework. It goes beyond the use of Spring and explores approaches to tackling resilience, security, and scalability concerns that will prove useful to any service designer. To make the best use of the code samples included in this book, readers should have basic knowledge of the Java language. Any previous experience with the Spring Framework would also help in getting up and running quickly.


In this book, you will find a number of text styles 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: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

import org.springframework.web.bind.annotation.*;

public class HelloWorldResource {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld() {
        return "Hello, world!";

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


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

mvn jetty:start

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "For example, with IntelliJ IDEA, our sample web service project can be imported by selecting the menu options File | Open."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.