Balaji Varanasi
Salt Lake City, UT, USA
Maxim Bartkov
Kharkov, Ukraine
ISBN 978-1-4842-7476-7 e-ISBN 978-1-4842-7477-4
https://doi.org/10.1007/978-1-4842-7477-4
Balaji Varanasi and Maxim Bartkov 2022
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Introduction
Spring REST serves as a practical guide for designing and developing RESTful APIs using the popular Spring Framework. This book begins with a brief introduction to REST, HTTP, and web infrastructure. It then provides detailed coverage of several Spring portfolio projects such as Spring Boot, Spring MVC, Spring Data JPA, and Spring Security. The book walks through the process of designing and building a REST application while taking a deeper look into design principles and best practices for versioning, security, documentation, error handling, paging, and sorting. It also discusses techniques for building clients that consume REST services. Finally, it covers Spring MVC Test frameworks for creating unit and integration tests for REST API.
After reading the book, you will have learned
About REST fundamentals and web infrastructure
About Spring technologies such as Spring Boot and Spring Data JPA
How to build REST applications with Spring technologies
How to identify REST resources and design their representations
Design principles for versioning REST services
How to document REST services using Swagger
Strategies for handling errors and communicating meaningful messages
Techniques for handling large datasets using pagination
Securing REST services using Basic Auth
How to build REST clients using RestTemplate
How to test REST services using the Spring MVC Test framework
How Is This Book Structured?
Chapter starts with an overview of REST. We cover REST fundamentals and abstractions such as resources and representations. We then discuss web infrastructure such as URIs, HTTP methods, and HTTP response codes. We also cover Richardsons Maturity Model, which provides a classification of REST services.
Chapter provides detailed coverage of Spring Web MVC. We begin with a gentle introduction to the Spring Framework and cover its two important conceptsdependency injection and aspect-oriented programming. Then we take a deeper look at the different components that make up Spring Web MVC.
Chapter introduces Spring Boot, a Spring project that simplifies the bootstrapping of Spring applications. We then use Spring Boot to build a Hello World REST application. Finally, we look at some tools that can be used to access REST applications.
Chapter discusses the beginnings of a RESTful application named QuickPoll. We analyze the requirements and design resources and their representations. Using Spring MVC components, we implement a set of RESTful services.
Chapter covers error handling in REST services. Well-designed, meaningful error responses play an important role in the adoption of REST services. We design a custom error response for QuickPoll and implement the design. We also add validation capabilities to the inputs provided by users. Finally, we look at techniques for externalizing the error messages to property files.
Chapter begins with an overview of the Swagger specification and its associated tools/frameworks. We then implement Swagger in QuickPoll to generate interactive documentation. We also customize Swagger and Swagger UI to meet our application requirements.
Chapter covers the different strategies for versioning a REST API. We then look at implementing versioning in QuickPoll using the URI versioning approach. We also review the different approaches for dealing with large datasets using pagination and sorting.
Chapter begins with a discussion of different strategies for securing REST services. We provide a detailed treatment of OAuth2 and review its different components. We then use the Spring Security framework to implement Basic Authentication in the QuickPoll application.
Chapter covers building REST clients and testing REST APIs. We use Springs RestTemplate features to build a REST client that works with different versions of the QuickPoll API. We then take a deeper look into the Spring MVC Test framework and examine its core classes. Finally, we write unit and integration tests to test the REST API.
Chapter discusses the HATEOAS constraint that allows developers to build flexible and loosely coupled REST services. It also covers the HAL hypermedia format. We then modify the QuickPoll application such that the Poll representations are generated following HATEOAS principles.
Target Audience
Spring REST is intended for enterprise and web developers using Java and Spring who want to build REST applications. The book requires a basic knowledge of Java, Spring, and the Web but no prior exposure to REST.
Downloading the Source Code
The source code for the examples in this book can be downloaded via the Download Source Code button located at www.apress.com/9781484274767 .
The downloaded source code contains a number of folders named ChapterX , in which X represents the corresponding chapter number. Each ChapterX folder contains two subfolders: a starter folder and a final folder. The starter folder houses a QuickPoll project that you can use as a basis to follow along the solution described in the corresponding chapter. Even though each chapter builds on the previous one, the starter project allows you to skip around the book. For example, if you are interested in learning about security, you can simply load the QuickPoll application under the Chapter8\starter folder and follow the solution described in Chapter . As the name suggests, the final folder contains the expected end state for that chapter.