Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building a RESTful Web Service with Spring
Building a RESTful Web Service with Spring

Building a RESTful Web Service with Spring: A hands-on guide to building an enterprise-grade, scalable RESTful web service using the Spring Framework

By Ludovic Dewailly
$25.99 $17.99
Book Oct 2015 128 pages 1st Edition
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly
eBook
$25.99 $17.99
Print
$32.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Oct 14, 2015
Length 128 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785285714
Vendor :
Pivotal
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Building a RESTful Web Service with Spring

Chapter 1. A Few Basics

With the prominence of the Internet and the ubiquity of HTTP in today's world, web services have become the main means for web-based systems to interoperate with each other. A web service is an interface that provides access to a web-facing system for clients and other services to consume.

Simple Object Access Protocol (SOAP) used to be the de facto choice for building such services. SOAP is an XML-based communication protocol, leveraging open standards. However, in recent years Representational State Transfer (REST) has become a very popular alternative to traditional SOAP web services. So, let's take a look at the principles behind REST. This chapter will cover the following topics:

  • Discussions of the REST principles

  • How the Spring Framework can help in the building of RESTful web services

  • A sample RESTful web service that will provide the backdrop for the rest of this book

REST principles


REST is a software architecture approach for creating scalable web services. The term REST was coined by Roy Fielding in his PhD dissertation, and revolves around a number of principles. These principles underpin the architecture of RESTful web services and are described in the following sections.

Uniform interface

At the core of REST are resources, and resources are identified using Uniform Resource Identifiers (URIs). Conceptually, resources are separate from their representation (that is, the format in which they are provided to clients). REST does not mandate any specific format, but typically includes XML and JSON (which will be discussed in Chapter 4, Data Representation).

In addition, resource representations are self-descriptive. In more concrete terms, this means that sufficient information must be returned for the successful processing of responses.

Another distinctive property of REST is that clients interact entirely through hypermedia, which is dynamically provided by the application servers. Apart from endpoints, clients need no prior knowledge of how to interact with a RESTful service. This constraint is referred to as Hypermedia as the Engine of Application State (HATEOAS).

Client-Server

The client-server model that REST embraces enables the separation of client concerns, such as user interaction or user state management, from that of server concerns such as data storage and scalability.

This decoupling ensures that, provided an interface that is agreed upon, the development of client and server can be done independently. It also helps reduce complexity and improve the effectiveness of performance tuning.

Stateless

REST advocates statelessness. No client state is stored on the server. All the information needed to perform operations is contained in the requests (as part of the URL, request body, or as HTTP headers).

Cacheable

RESTful web services must provide caching capabilities. Servers can indicate how and for how long to cache responses. Clients can use cached responses instead of contacting the server.

Tip

This principle has significant advantages for scalability. Caching techniques will be discussed in Chapter 6, Performance.

Since REST typically leverages HTTP, it inherits all the caching properties that HTTP offers.

Layered system

Given the style of communication between clients and servers, clients are not aware of which specific server they are interacting with. This property allows the introduction of intermediate servers that can, for example, handle security or offer load-balancing capabilities. These architectural concepts are discussed in more detail in Chapter 10, Scaling a RESTful Web Service.

Code on demand

Even though it's part of the REST architecture, this principal is optional. Servers can temporarily extend the functionality of clients by transferring executable code. For example, JavaScript can be provided to web-based clients to customize functionality.

For a service to be considered RESTful, it should abide by the preceding principles.

The Spring Framework and REST


It is assumed that the reader is familiar with the Spring Framework (referred to as Spring from here on). We will therefore focus on the specificities of building RESTful web services with Spring, in this section.

Since REST hinges on URIs, the Spring Web MVC framework provides all the necessary tools for building RESTful endpoints. Annotations, such as org.springframework.web.bind.annotation.RequestMapping and org.springframework.web.bind.annotation.RequestParam for mapping URLs and parameters form the basis for creating such endpoints. Chapter 3, The First Endpoint, will discuss these annotations and offer code samples to illustrate their use.

Note

Reference documentation about the Spring Web MVC can be found at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html.

With the technological context laid out, let's now look at one such RESTful service. Throughout this book, we will be building a sample web service that helps manage hotels and B&Bs.

Our RESTful web service


One common piece of software in use in the hospitality industry is a property management system (careful readers will notice the unfortunate acronym for these systems). It allows automating the operations of hotels and B&Bs. For the purpose of this book, we will build such a system using Spring. Each component of this system will expose a RESTful API that will be consumed by a simple web interface.

Tip

Designing effective Application Programming Interfaces is a topic that deserves its own treaty. It is beyond the scope of this book to discuss these concerns in detail. The main characteristics that one should bear in mind when designing APIs are: ease of use, consistency, exposing as little as necessary, extensibility, and forward compatibility.

Architecture

Our property management system will be formed of the four components, as illustrated in the following figure:

The four components are explained as follows:

  • Inventory Service: This component provides the necessary functionality to manage and describe rooms and room types.

  • Availability Service: This component lets users see what rooms are available on specific dates.

  • Booking Service: This component will be responsible for taking bookings. It will rely on the Inventory Service and Availability Service components to validate bookings.

  • Billing Service: Once a booking is made, this component will offer the ability to generate an invoice.

Data model

In this section, we will look at the data model that will support our web service. The following entity relationship diagram provides an overview of this model:

The entities that constitute our model are as follows:

  • Room: This object represents the physical rooms that are part of our hotel. Rooms have a name and a description as well as photos.

  • RoomCategory: Each room belongs to a category (for example, double room). Categories provide a description, and are linked to a pricing model.

  • Pricing: This object encapsulates how rooms are priced (for example, a fixed price, or a sliding price based on the number of guests).

  • Booking: The reservation of rooms is modeled on bookings. Bookings will capture the reserved room, dates, and contact details for guests.

  • Invoice: This provides invoices to guests upon booking. They contain the relevant information regarding the booking, and the amount to be settled.

The data access layer will be implemented using Object-Relational Mapping (ORM) with Hibernate 4.3.8.

Tip

We will not delve into the specificities of Hibernate ORM in this book. However, documentation is available on Hibernate.org at http://hibernate.org/orm/.

In addition, for the purpose of simplifying the development and testing of our web service, we will use an embedded H2 database.

Note

Documentation about H2 can be found at http://www.h2database.com.

Hibernate supports H2 out of the box, so no specific setup is required to use it as our embedded database.

Tip

While an embedded database is great for development, it is not fit for a production deployment.

Summary


Now that we had a look at the topics that we will cover in this book, and what our RESTful web service will do, let's discuss how we are going to build it.

In the next chapter, we will see the description of several tools available to help us create our sample web service.

Left arrow icon Right arrow icon

Key benefits

What you will learn

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Oct 14, 2015
Length 128 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785285714
Vendor :
Pivotal
Category :
Concepts :

Table of Contents

17 Chapters
Building a RESTful Web Service with Spring Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
A Few Basics Chevron down icon Chevron up icon
Building RESTful Web Services with Maven and Gradle Chevron down icon Chevron up icon
The First Endpoint Chevron down icon Chevron up icon
Data Representation Chevron down icon Chevron up icon
CRUD Operations in REST Chevron down icon Chevron up icon
Performance Chevron down icon Chevron up icon
Dealing with Security Chevron down icon Chevron up icon
Testing RESTful Web Services Chevron down icon Chevron up icon
Building a REST Client Chevron down icon Chevron up icon
Scaling a RESTful Web Service Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.