Page
1
Pro Spring Boot 3 An Authoritative Guide with Best Practices — Third Edition — Felipe Gutierrez
Page
2
Pro Spring Boot 3 An Authoritative Guide with Best Practices Third Edition Felipe Gutierrez
Page
3
Pro Spring Boot 3: An Authoritative Guide with Best Practices, Third Edition ISBN-13 (pbk): 978-1-4842-9293-8 ISBN-13 (electronic): 978-1-4842-9294-5 https://doi.org/10.1007/978-1-4842-9294-5 Copyright © 2024 by Felipe Gutierrez 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. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the author nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Melissa Duffy Development Editor: Jim Markham Coordinating Editor: Gryffin Winkler Cover image designed by kentaro-tachikawa on Unsplash (https://unsplash.com/) Distributed to the book trade worldwide by Springer Science+Business Media LLC, 1 New York Plaza, Suite 4600, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm. com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at https://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on the GitHub repository. For more detailed information, please visit https://www.apress.com/gp/ services/source-code. If disposing of this product, please recycle the paper Felipe Gutierrez 4109 Rillcrest Grove Way Fuquay Varina, NC 27526-3562 Albuquerque, NM, USA
Page
4
Dedicated in loving memory of my aunt, Fabiola Cerón, and Simón Cruz, my uncle; I miss you so much! Thanks uncle, for everything you taught me!
Page
5
(This page has no text content)
Page
6
(This page has no text content)
Page
7
(This page has no text content)
Page
8
(This page has no text content)
Page
9
(This page has no text content)
Page
10
(This page has no text content)
Page
11
(This page has no text content)
Page
12
xiii About the Author Felipe Gutierrez is a solutions software architect with bachelor’s and master’s degrees in computer science from Instituto Tecnologico y de Estudios Superiores de Monterrey Campus Ciudad de Mexico. Felipe has over 25 years of IT experience and has developed programs for companies in multiple vertical industries, such as government, retail, healthcare, education, and banking. He is currently working as Staff Engineer for VMware, specializing in content development for Tanzu Learning and the new Spring Academy learning site, Spring Framework, Spring Cloud Native Applications, Groovy, and RabbitMQ, among other technologies. He has also worked as a solutions architect for big companies like Nokia, Apple, Redbox, IBM, and Qualcomm. He is the author of Spring Boot Messaging (Apress, 2017) and Spring Cloud Data Flow (Apress, 2020).
Page
13
xv Manuel Jordan Elera is an autodidactic developer and researcher who enjoys learning new technologies for his own experiments and creating new integrations. Manuel won the Springy Award – Community Champion and Spring Champion 2013. In his little free time, he reads the Bible and composes music on his guitar. Manuel is known as dr_pompeii. He has tech-reviewed numerous books, including Pro Spring MVC with WebFlux (Apress, 2020), Pro Spring Boot 2 (Apress, 2019), Rapid Java Persistence and Microservices (Apress, 2019), Java Language Features (Apress, 2018), Spring Boot 2 Recipes (Apress, 2018), and Java APIs, Extensions and Libraries (Apress, 2018). You can read his detailed tutorials on Spring technologies and contact him through his blog at www.manueljordanelera.blogspot.com. You can follow Manuel on his Twitter account, @dr_pompeii. About the Technical Reviewer
Page
14
xvii Acknowledgments Thanks to the Spring team for creating such an amazing Java Framework! Thanks to my technical reviewer, Manuel Jordan, for always excelling in his reviews! I also want to thank the Apress editorial team for their patience and excellent work. Finally, I want to thank all of my family for their support and a special dedication to my loving aunt Fabiola and my super awesome uncle Simon (wife and husband), who passed away too soon! We miss you.
Page
16
3 © Felipe Gutierrez 2024 F. Gutierrez, Pro Spring Boot 3, https://doi.org/10.1007/978-1-4842-9294-5_1 CHAPTER 1 Spring Boot Quick Start Welcome to the first chapter of the book, which will quickly immerse you in Spring Boot and demonstrate how easy it is to use by walking you through a simple project that exposes an API over the Web. If you are new to Spring Boot, this chapter will help you to rapidly familiarize yourself with the framework. If you are an experienced developer, feel free to quickly review the setup of the project (which will be referenced throughout this book) and move on to the next chapter. Project: Users App The project that we are going to build, named Users App, will expose a simple CRUD (create, read, update, and delete) API over the Web. These are the requirements for the Users App project: • A user must have a name and an email address. • A map is used to hold the information, using the email address as the key. • It exposes an API that uses CRUD over the Web. Initial Setup To start with Spring Boot, you need to have the following installed: • Java: You can install, for example, OpenJDK (https://jdk.java. net/archive/) or Eclipse Temurin (https://adoptium.net/ temurin/releases/). • If you are Unix user, you can use SDKMAN! (https://sdkman.io/), which works for Linux and macOS.
Page
17
4 • If you are Windows user, you can use Chocolatey (https://chocolatey.org/). • An integrated development environment (IDE): As a suggestion, you can use Microsoft Visual Studio Code (https://code.visualstudio. com/download), the Community edition of IntelliJ IDEA from JetBrains (https://www.jetbrains.com/idea/download/), or Spring Tools (https://spring.io/tools). • The curl or http command: For http, you can install HTTPie (https://httpie.io/). Both commands are demonstrated later in this chapter. • The jq command: You can install it using the instructions at https:// stedolan.github.io/jq/. Start @ start.spring.io start.spring.io is the official web-based tool for generating Spring Boot projects. It provides a user-friendly interface to quickly set up a new Spring Boot project with your desired dependencies and configurations. Key features and benefits: • Streamlined project creation: Eliminates the need to manually configure a project structure and dependencies. • Curated dependencies: Offers a selection of common libraries and frameworks to easily add to your project. • Customization: Allows you to choose the build tool (Maven or Gradle), language (Java, Kotlin, Groovy), and Spring Boot version. • Downloadable project: Generates a ZIP file containing the configured project ready to be imported into your IDE. • Spring Boot integration: Leverages Spring Boot’s auto-configuration and convention-over-configuration principles for rapid development. Open a browser and go to https://start.spring.io . You should see the home page of the Spring Initializr, as shown in Figure 1-1. Chapter 1 Spring Boot QuiCk Start
Page
18
5 Figure 1-1. Spring Initializr home page Notice that by default the Spring Initializr uses Gradle – Groovy as the project builder, Java as the programming language, JAR for packaging, Java 17, and Spring Boot 3 (at the time of this writing, I’m using Spring Boot 3.2.3). Modify the Project Metadata section with the following values, as shown in Figure 1-2 (The value of the Package name field will change automatically based on the values of the Group and Artifact fields). • Group: com.apress • Artifact: users • Name: users • Dependencies: Spring Web (click Add to find it) Chapter 1 Spring Boot QuiCk Start
Page
19
6 Figure 1-2. Spring Initializr: Users App project Figure 1-2 shows all the necessary information to create the Users App project. Click the Generate button to zip the project and save it to your computer. Then, unzip it and import it to your favorite IDE. (I am using IntelliJ IDEA Community Edition, so that is what you will see in the screenshots in this book.) Note You can download or fork the source code from the apress github site. Chapter 1 Spring Boot QuiCk Start
Page
20
7 When you open the project in your IDE, you should see the project structure shown in Figure 1-3. Figure 1-3. Users App project structure Figure 1-3 shows the following three folders in the project structure: • src/main/java: This folder contains all the source code. By default, the Spring Initializr creates the UsersApplication.java file. This has the main entry point where the application will start. • src/main/resources: This folder contains one of the most important files, application.properties, which is used to modify configuration. We will use this file repeatedly throughout the book; for now, just know that it’s located in this folder. This folder also Chapter 1 Spring Boot QuiCk Start