Balaji Varanasi and Maxim Bartkov

Spring REST

Building Java Microservices and Cloud Applications

2nd ed.
Balaji Varanasi
Salt Lake City, UT, USA
Maxim Bartkov
Kharkov, Ukraine
ISBN 978-1-4842-7476-7e-ISBN 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 1 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 Richardson’s Maturity Model, which provides a classification of REST services.

Chapter 2 provides detailed coverage of Spring Web MVC. We begin with a gentle introduction to the Spring Framework and cover its two important concepts—dependency injection and aspect-oriented programming. Then we take a deeper look at the different components that make up Spring Web MVC.

Chapter 3 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 4 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 5 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 6 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 7 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 8 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 9 covers building REST clients and testing REST APIs. We use Spring’s 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 10 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 Chapter8starter folder and follow the solution described in Chapter 8. As the name suggests, the final folder contains the expected end state for that chapter.

Chapters 1 and 2 don’t have any associated code. Therefore, the corresponding ChapterX folders for those chapters contain empty starter and final folders. In Chapter 3, we build a Hello World application, so Chapter 3’s starter and final folders contain the hello-rest application. Starting from Chapter 4, the starter and final folders contain QuickPoll project source code.

Contacting the Authors

We always welcome feedback from our readers. If you have any questions or suggestions regarding the contents of this book, you can contact the authors at [email protected] or [email protected].

Acknowledgments

This book would not have been possible without the support of several people, and we would like to take this opportunity to sincerely thank them.

Thanks to the amazing folks at Apress; without you, this book would not have seen the light of day. Thanks to Mark Powers for being patient and keeping us focused. Thanks to Steve Anglin for his constant support and the rest of the Apress team involved in this project.

Huge thanks to our technical reviewer Rohan Walia for his efforts and attention to detail. His valuable feedback has led to many improvements in the book.

Finally, we would like to thank our friends and family for their constant support and encouragement.

Table of Contents
Index 237
About the Authors
Balaji Varanasi
../images/332520_2_En_BookFrontmatter_Figb_HTML.jpg
is a software development manager, author, speaker, and technology entrepreneur. He has over 14 years of experience designing and developing high-performance, scalable Java and .NET mobile applications. He has worked in the areas of security, web accessibility, search, and enterprise portals. He has a master’s degree in computer science from Utah State University and serves as adjunct faculty at the University of Phoenix, teaching programming and information system courses. He has authored Apress’s Practical Spring LDAP and has coauthored Introducing Maven.
 
Maxim Bartkov
../images/332520_2_En_BookFrontmatter_Figc_HTML.jpg
is a staff engineer with more than seven years of commercial experience in Java. Maxim specializes in building architecture for high-load systems. He is skilled in the development of distributed high-load systems, microservice architecture, Spring Framework, and system architecture. In his spare time, he writes articles for the Java community.
 
About the Technical Reviewer
Rohan Walia
../images/332520_2_En_BookFrontmatter_Figd_HTML.jpg
is a senior software consultant with extensive experience in client-server, web-based, and enterprise application development. He is an Oracle Certified ADF Implementation Specialist and a Sun Certified Java programmer. Rohan is responsible for designing and developing end-to-end applications consisting of various cutting-edge frameworks and utilities. His areas of expertise are Oracle ADF, Oracle WebCenter, Fusion, Spring, Hibernate, and Java/J2EE. When not working, Rohan loves to play tennis, hike, and travel. Rohan would like to thank his wife, Deepika Walia, for using all her experience and expertise to review this book.
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset