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.
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.
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
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.
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.
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.
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].
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.