📄 Page
1
Pro Spring Boot 3 with Kotlin In-Depth Guide to Best Practices for Cloud-Native and Microservices Development — Third Edition — Peter Späth Felipe Gutierrez
📄 Page
2
Pro Spring Boot 3 with Kotlin In-Depth Guide to Best Practices for Cloud-Native and Microservices Development Third Edition Peter Späth Felipe Gutierrez
📄 Page
3
Pro Spring Boot 3 with Kotlin: In-Depth Guide to Best Practices for Cloud-Native and Microservices Development, Third Edition ISBN-13 (pbk): 979-8-8688-1130-2 ISBN-13 (electronic): 979-8-8688-1131-9 https://doi.org/10.1007/979-8-8688-1131-9 Copyright © 2025 by Peter Späth, 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 authors 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 Desk Editor: Laura Berendson Editorial Project Manager: Gryffin Winkler Copy Editor: Kezia Endsley Cover designed by eStudioCalamar Cover image designed by Image by esiuL from Pixabay Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. 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 http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit https://www.apress.com/gp/services/ source-code. If disposing of this product, please recycle the paper Peter Späth Leipzig, Sachsen, Germany Felipe Gutierrez Albuquerque, NM, USA
📄 Page
5
v Table of Contents About the Authors ����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Introduction ������������������������������������������������������������������������������������������������������������xix Part I: Introduction ����������������������������������������������������������������������������������������� 1 Chapter 1: Spring Boot Quick Start �������������������������������������������������������������������������� 3 Project: Users App ������������������������������������������������������������������������������������������������������������������������� 3 Initial Setup ����������������������������������������������������������������������������������������������������������������������������� 3 Start @ start�spring�io ������������������������������������������������������������������������������������������������������������� 4 Testing the Users App Project ������������������������������������������������������������������������������������������������ 14 Running the Users App Project ���������������������������������������������������������������������������������������������� 19 Why Spring Boot?������������������������������������������������������������������������������������������������������������������������ 25 Spring Boot Features ������������������������������������������������������������������������������������������������������������� 28 Summary������������������������������������������������������������������������������������������������������������������������������������� 30 Chapter 2: Spring Boot Internals ���������������������������������������������������������������������������� 31 Requirements to Create a Spring Boot App ��������������������������������������������������������������������������������� 31 Project: My Retro App ������������������������������������������������������������������������������������������������������������������ 33 Auto-Configuration ���������������������������������������������������������������������������������������������������������������������� 40 @EnableAutoConfiguration, @Enable<Technology>, and @Conditional* Annotations ��������� 44 Spring Boot Features ������������������������������������������������������������������������������������������������������������������ 48 Custom Banner ���������������������������������������������������������������������������������������������������������������������� 50 SpringApplicationBuilder ������������������������������������������������������������������������������������������������������� 57 Application Arguments ���������������������������������������������������������������������������������������������������������� 60 Executable JAR ���������������������������������������������������������������������������������������������������������������������� 62 ApplicationRunner, CommandLineRunner, and ApplicationReadyEvent �������������������������������� 62
📄 Page
6
vi Application Configuration ������������������������������������������������������������������������������������������������������ 65 My Retro App and Users App Project Integration ������������������������������������������������������������������� 65 Application Profiles ���������������������������������������������������������������������������������������������������������������� 73 Summary������������������������������������������������������������������������������������������������������������������������������������� 74 Chapter 3: Spring Boot Web Development �������������������������������������������������������������� 75 Spring MVC ��������������������������������������������������������������������������������������������������������������������������������� 75 Spring Boot MVC Auto-Configuration ������������������������������������������������������������������������������������������ 76 My Retro App Project ������������������������������������������������������������������������������������������������������������������ 78 Adding Dependencies ������������������������������������������������������������������������������������������������������������ 81 AOP to the Rescue ����������������������������������������������������������������������������������������������������������������� 92 Spring Web Annotated Controllers ����������������������������������������������������������������������������������������� 95 Testing My Retro App ����������������������������������������������������������������������������������������������������������� 102 Users App Project ���������������������������������������������������������������������������������������������������������������������� 108 Spring Web Functional Endpoints ���������������������������������������������������������������������������������������� 114 Testing the Users App ���������������������������������������������������������������������������������������������������������� 119 Spring Boot Web: Overriding Defaults ��������������������������������������������������������������������������������������� 123 Overriding Default Server Settings �������������������������������������������������������������������������������������� 124 JSON Date Format ��������������������������������������������������������������������������������������������������������������� 125 Using a Different Application Container ������������������������������������������������������������������������������� 126 Spring Boot Web Clients ������������������������������������������������������������������������������������������������������������ 126 Testing the Client ����������������������������������������������������������������������������������������������������������������� 130 Summary����������������������������������������������������������������������������������������������������������������������������������� 131 Chapter 4: Spring Boot SQL Data Access �������������������������������������������������������������� 133 Spring Boot Features for SQL Databases ���������������������������������������������������������������������������������� 133 Spring Framework Data Access ������������������������������������������������������������������������������������������������ 134 JDBC with Spring Boot �������������������������������������������������������������������������������������������������������������� 135 Users App: Using Spring Boot JDBC ������������������������������������������������������������������������������������������ 136 Gravatar: Identifying the User ��������������������������������������������������������������������������������������������������� 139 Model: enum and record Types ������������������������������������������������������������������������������������������������� 139 JdbcTemplate and RowMapper������������������������������������������������������������������������������������������������� 141 Table of ConTenTs
📄 Page
7
vii Adding the Web Controller �������������������������������������������������������������������������������������������������������� 144 Adding Users when the App Is Ready ��������������������������������������������������������������������������������������� 147 Database Initialization ��������������������������������������������������������������������������������������������������������������� 149 Testing the Users App ��������������������������������������������������������������������������������������������������������������� 150 But Wait��� What Happened with the Tests? ������������������������������������������������������������������������������ 153 Running the Users App �������������������������������������������������������������������������������������������������������������� 154 Using PostgreSQL ��������������������������������������������������������������������������������������������������������������������� 157 My Retro App: Using Spring Boot JDBC ������������������������������������������������������������������������������������� 160 Running the My Retro App �������������������������������������������������������������������������������������������������������� 179 But Wait��� What��� Again? ���������������������������������������������������������������������������������������������������������� 181 Summary����������������������������������������������������������������������������������������������������������������������������������� 181 Chapter 5: Spring Data with Spring Boot�������������������������������������������������������������� 183 Spring Data ������������������������������������������������������������������������������������������������������������������������������� 183 Spring Data JDBC ���������������������������������������������������������������������������������������������������������������������� 184 Spring Data JDBC with Spring Boot������������������������������������������������������������������������������������� 185 Spring Data JPA ������������������������������������������������������������������������������������������������������������������������ 211 Spring Data JPA with Spring Boot ��������������������������������������������������������������������������������������� 212 Spring Data REST ���������������������������������������������������������������������������������������������������������������������� 229 Spring Data REST with Spring Boot ������������������������������������������������������������������������������������� 230 Summary����������������������������������������������������������������������������������������������������������������������������������� 253 Chapter 6: Spring Data NoSQL with Spring Boot �������������������������������������������������� 255 Spring Data MongoDB ��������������������������������������������������������������������������������������������������������������� 255 Spring Data MongoDB with Spring Boot ����������������������������������������������������������������������������������� 256 Users App with Spring Data MongoDB and Spring Boot ����������������������������������������������������������� 256 Testing the Users App ��������������������������������������������������������������������������������������������������������������� 264 Running the Users App �������������������������������������������������������������������������������������������������������������� 267 My Retro App with Spring Data MongoDB Using Spring Boot ��������������������������������������������������� 269 Running My Retro App �������������������������������������������������������������������������������������������������������������� 280 Spring Data Redis ��������������������������������������������������������������������������������������������������������������������� 285 Table of ConTenTs
📄 Page
8
viii Spring Data Redis with Spring Boot ������������������������������������������������������������������������������������������ 286 Users App with Spring Data Redis Using Spring Boot ��������������������������������������������������������������� 286 Testing the Users App ��������������������������������������������������������������������������������������������������������������� 293 Running the Users App �������������������������������������������������������������������������������������������������������������� 296 My Retro App with Spring Data Redis Using Spring Boot ��������������������������������������������������������� 298 Running My Retro App �������������������������������������������������������������������������������������������������������������� 302 Summary����������������������������������������������������������������������������������������������������������������������������������� 305 Chapter 7: Spring Boot Reactive ��������������������������������������������������������������������������� 307 Reactive Systems ���������������������������������������������������������������������������������������������������������������������� 307 The Reactive Manifesto������������������������������������������������������������������������������������������������������������� 307 Project Reactor ������������������������������������������������������������������������������������������������������������������������� 309 Mono<T>, an Asynchronous [0|1] Result ���������������������������������������������������������������������������������� 309 Flux<T>: An Asynchronous Sequence of [0|N] Items ���������������������������������������������������������������� 310 Reactive Web and Data Applications with Spring Boot ������������������������������������������������������������� 310 Users App with Spring Boot Reactive ���������������������������������������������������������������������������������������� 312 Testing the Users App ��������������������������������������������������������������������������������������������������������������� 322 Running the Users App �������������������������������������������������������������������������������������������������������������� 325 My Retro App with Spring Boot Reactive ���������������������������������������������������������������������������������� 326 Running My Retro App �������������������������������������������������������������������������������������������������������������� 340 Summary����������������������������������������������������������������������������������������������������������������������������������� 341 Chapter 8: Spring Boot Testing ����������������������������������������������������������������������������� 343 Spring Testing Framework �������������������������������������������������������������������������������������������������������� 343 Spring Boot Testing Framework ������������������������������������������������������������������������������������������������ 345 Testing Web Apps with a Mock Environment ����������������������������������������������������������������������� 346 Using Mocking and Spying Beans ��������������������������������������������������������������������������������������� 350 Testcontainers ��������������������������������������������������������������������������������������������������������������������� 354 Spring Boot Testing Slices ��������������������������������������������������������������������������������������������������� 356 Using Testcontainers to Run Your Spring Boot Applications ������������������������������������������������������ 377 Summary����������������������������������������������������������������������������������������������������������������������������������� 379 Table of ConTenTs
📄 Page
9
ix Part II: Spring Cloud Data Flow: Internals �������������������������������������������������� 381 Chapter 9: Spring Boot Security ��������������������������������������������������������������������������� 383 Spring Security ������������������������������������������������������������������������������������������������������������������������� 383 Spring Security with Spring Boot ���������������������������������������������������������������������������������������� 386 Adding Security to Users App ���������������������������������������������������������������������������������������������������� 387 Users App Web Controller ���������������������������������������������������������������������������������������������������� 393 Testing Security in Users App ���������������������������������������������������������������������������������������������� 393 Mocking Security Tests �������������������������������������������������������������������������������������������������������� 396 Using Persistence for Security Authentication in Users App ����������������������������������������������� 398 Connecting a UI to Users App ���������������������������������������������������������������������������������������������� 405 Adding Security to My Retro App ���������������������������������������������������������������������������������������������� 412 Unit Testing My Retro App Security for Authorization ���������������������������������������������������������� 425 Putting Everything Together: UI, Users App, and Retro App ������������������������������������������������������� 430 Adding a Social Login with OAuth2 ������������������������������������������������������������������������������������������� 433 Social Login to Users App ���������������������������������������������������������������������������������������������������� 434 Running Users App with Social Login ���������������������������������������������������������������������������������� 442 The Spring Authorization Server ����������������������������������������������������������������������������������������������� 445 Getting Started �������������������������������������������������������������������������������������������������������������������� 446 Using the Spring Authentication Server with Users App������������������������������������������������������ 451 Wait��� What About My Retro App? ��������������������������������������������������������������������������������������� 460 Summary����������������������������������������������������������������������������������������������������������������������������������� 461 Chapter 10: Messaging with Spring Boot ������������������������������������������������������������� 463 Messaging As a Concept ����������������������������������������������������������������������������������������������������������� 463 Messaging with Spring Boot ����������������������������������������������������������������������������������������������� 464 Events with Spring Boot ������������������������������������������������������������������������������������������������������������ 466 Adding Events to Users App ������������������������������������������������������������������������������������������������� 467 JMS with Spring Boot ��������������������������������������������������������������������������������������������������������������� 470 Which Broker Are We Using? ����������������������������������������������������������������������������������������������� 472 Users App with JMS ������������������������������������������������������������������������������������������������������������ 472 Using JMS Topics with My Retro App ���������������������������������������������������������������������������������� 486 Table of ConTenTs
📄 Page
10
x AMQP with Spring Boot ������������������������������������������������������������������������������������������������������������� 494 Installing RabbitMQ ������������������������������������������������������������������������������������������������������������� 494 RabbitMQ/AMQP: Exchanges, Bindings, and Queues ���������������������������������������������������������� 495 Using AMQP with RabbitMQ in Users App ���������������������������������������������������������������������������� 497 Using RabbitMQ in My Retro App to Consume User Events ������������������������������������������������� 516 WebSockets with Spring Boot ��������������������������������������������������������������������������������������������������� 522 Adding WebSockets to Users App ���������������������������������������������������������������������������������������� 523 Using WebSockets in My Retro App to Consume Events ����������������������������������������������������� 531 RSocket with Spring Boot ��������������������������������������������������������������������������������������������������������� 541 Using RSocket with Users App with Spring Boot ����������������������������������������������������������������� 542 Requesting RSocket Messages in My Retro App ����������������������������������������������������������������� 546 Other Messaging Frameworks �������������������������������������������������������������������������������������������������� 553 Summary����������������������������������������������������������������������������������������������������������������������������������� 553 Chapter 11: Spring Boot Actuator ������������������������������������������������������������������������� 555 What Is Spring Boot Actuator? �������������������������������������������������������������������������������������������������� 555 Users App with Spring Boot Actuator ���������������������������������������������������������������������������������������� 556 /actuator ������������������������������������������������������������������������������������������������������������������������������ 559 Adding Security ������������������������������������������������������������������������������������������������������������������� 582 Implementing Custom Actuator Endpoints �������������������������������������������������������������������������� 588 More Spring Boot Actuator Configuration ���������������������������������������������������������������������������� 599 Overview of /actuator/health ����������������������������������������������������������������������������������������������� 601 Health Indicators ����������������������������������������������������������������������������������������������������������������� 603 My Retro App with Spring Boot Actuator Observability: Metrics, Logs, and Tracing ����������������� 610 Adding Observability to My Retro App ��������������������������������������������������������������������������������� 610 What About External Resources: Memory, CPU, and Storage? �������������������������������������������� 641 Summary����������������������������������������������������������������������������������������������������������������������������������� 648 Chapter 12: Spring Boot Native and AOT �������������������������������������������������������������� 649 What Is Spring AOT? ������������������������������������������������������������������������������������������������������������������ 649 What Is GraalVM? ���������������������������������������������������������������������������������������������������������������������� 651 Benefits of Using GraalVM ��������������������������������������������������������������������������������������������������� 651 Examples of Using GraalVM ������������������������������������������������������������������������������������������������� 652 Table of ConTenTs
📄 Page
11
xi Spring, Spring Boot, and GraalVM ��������������������������������������������������������������������������������������������� 654 AOT Processing in Spring ���������������������������������������������������������������������������������������������������� 655 Creating GraalVM Native Apps �������������������������������������������������������������������������������������������������� 657 Creating a Native Users App ������������������������������������������������������������������������������������������������ 657 Creating a Native My Retro App ������������������������������������������������������������������������������������������� 668 GraalVM Native Images, wait��� What? �������������������������������������������������������������������������������������� 677 Creating Native Image for Users App ����������������������������������������������������������������������������������� 678 Creating a My Retro App Native Image �������������������������������������������������������������������������������� 680 Testing Native Apps and Images ����������������������������������������������������������������������������������������������� 682 AOT Processing with Custom Hints ������������������������������������������������������������������������������������������� 682 Summary����������������������������������������������������������������������������������������������������������������������������������� 684 Chapter 13: Spring Cloud with Spring Boot ���������������������������������������������������������� 685 Challenges Addressed by Spring Cloud ������������������������������������������������������������������������������������ 685 Microservices ���������������������������������������������������������������������������������������������������������������������������� 687 Twelve-Factor App Practices and Spring Boot/Spring Cloud Relationship�������������������������������� 688 Cloud Development ������������������������������������������������������������������������������������������������������������������� 691 Using Spring Cloud Technologies ���������������������������������������������������������������������������������������������� 693 HashiCorp Consul and Spring Cloud Consul ������������������������������������������������������������������������ 695 Reviewing the UserProperties ��������������������������������������������������������������������������������������������� 711 Using a YAML Format Instead of a Key/Value Pair as a Configuration ��������������������������������� 713 Using Spring Cloud Consul in My Retro App ����������������������������������������������������������������������������� 716 Using OpenFeign in My Retro App ��������������������������������������������������������������������������������������������� 719 Running My Retro App �������������������������������������������������������������������������������������������������������������� 727 HashiCorp Vault and Spring Cloud Vault ������������������������������������������������������������������������������������ 728 HashiCorp Vault ������������������������������������������������������������������������������������������������������������������� 728 Using HashiCorp Vault with Credentials Creation for PostgreSQL ��������������������������������������� 730 Spring Cloud Vault ��������������������������������������������������������������������������������������������������������������� 736 Implementing Credential Generation on the Users Service App Using HashiCorp Vault ������ 738 Running Users App with Vault ��������������������������������������������������������������������������������������������� 740 Fixing Credential Rotation by Adding a Listener ������������������������������������������������������������������ 741 Table of ConTenTs
📄 Page
12
xii Using Spring Cloud Gateway ����������������������������������������������������������������������������������������������������� 745 Creating a My Retro Gateway ���������������������������������������������������������������������������������������������� 746 Running the My Retro Gateway ������������������������������������������������������������������������������������������� 750 More Gateway Features ������������������������������������������������������������������������������������������������������� 754 Integrating the Cloud Environment into Docker Compose �������������������������������������������������������� 758 Creating Multi-Architecture Images ������������������������������������������������������������������������������������������ 763 Using a Cloud Platform: Kubernetes ����������������������������������������������������������������������������������������� 765 Using Kubernetes ���������������������������������������������������������������������������������������������������������������� 767 Creating the Postgres Deployment �������������������������������������������������������������������������������������� 768 Installing the Declarations ��������������������������������������������������������������������������������������������������� 771 Deploying Microservices in Kubernetes: Users App, My Retro App, and My Retro Gateway App Deployments ���������������������������������������������������������������������������������������� 772 ClusterRole and ClusterRoleBinding ������������������������������������������������������������������������������������ 772 Users Deployment and Service ������������������������������������������������������������������������������������������� 773 My Retro Deployment and Service �������������������������������������������������������������������������������������� 776 My Retro Gateway ConfigMap, Deployment, and Service ���������������������������������������������������� 778 Access to My Retro Gateway: Using Port-Forward �������������������������������������������������������������� 782 Cleaning Up ������������������������������������������������������������������������������������������������������������������������� 782 Summary����������������������������������������������������������������������������������������������������������������������������������� 783 Chapter 14: Extending Spring Boot����������������������������������������������������������������������� 785 Benefits of a Custom Starter ����������������������������������������������������������������������������������������������������� 785 Revisiting @Conditional and @Enable �������������������������������������������������������������������������������������� 786 Requirements of the Custom Spring Boot Starter ��������������������������������������������������������������������� 788 Rules and Guidelines for a Custom Spring Boot Starter ����������������������������������������������������������� 790 Creating My Retro Spring Boot Starter �������������������������������������������������������������������������������������� 791 Building the myretro-spring-boot-starter Custom Starter �������������������������������������������������������� 812 Publishing the Custom Starter in GitHub as a Maven Artifact ��������������������������������������������������� 813 Using the myretro-spring-boot-starter Custom Starter ������������������������������������������������������������ 818 Running Users App with myretro-spring-boot-starter �������������������������������������������������������������� 826 Summary����������������������������������������������������������������������������������������������������������������������������������� 828 Table of ConTenTs
📄 Page
13
xiii Chapter 15: Spring Boot New Projects ����������������������������������������������������������������� 831 Spring Modulith ������������������������������������������������������������������������������������������������������������������������� 831 Comparing Spring Modulith with Microservices ����������������������������������������������������������������������� 831 Choosing the Right Approach ���������������������������������������������������������������������������������������������������� 832 Fundamentals ��������������������������������������������������������������������������������������������������������������������������� 833 Understanding Module Packages in Spring Boot ���������������������������������������������������������������������� 835 Using Spring Modulith in the My Retro Solution ����������������������������������������������������������������������� 836 Running the My Retro Solution ������������������������������������������������������������������������������������������������� 848 Spring AI ������������������������������������������������������������������������������������������������������������������������������������ 849 AI Concepts ������������������������������������������������������������������������������������������������������������������������������� 849 Models ��������������������������������������������������������������������������������������������������������������������������������� 849 Prompts ������������������������������������������������������������������������������������������������������������������������������� 850 Prompt Templates ���������������������������������������������������������������������������������������������������������������� 851 Embeddings ������������������������������������������������������������������������������������������������������������������������� 851 Tokens ��������������������������������������������������������������������������������������������������������������������������������� 852 Teaching AI New Tricks: Beyond the Training Dataset ��������������������������������������������������������������� 853 Creating a Chat GPT Client��������������������������������������������������������������������������������������������������� 853 Running the App ������������������������������������������������������������������������������������������������������������������ 856 Summary����������������������������������������������������������������������������������������������������������������������������������� 856 Appendix A: Spring Boot Migration ���������������������������������������������������������������������� 857 Appendix B: Spring Boot GraphQL ������������������������������������������������������������������������ 869 Index ��������������������������������������������������������������������������������������������������������������������� 897 Table of ConTenTs
📄 Page
14
xv About the Authors Felipe Gutierrez is a senior platform architect at VMware, the creators of Spring Boot and Spring Framework, where he serves as a senior consultant with the Spring team. He has more than 25 years of IT experience, during which time he developed programs for companies in multiple vertical industries such as government, retail, healthcare, education, and banking. He also develops in Kotlin, Groovy, RabbitMQ, and other technologies. He has also consulted for companies such as Nokia, Apple, Redbox, and Qualcomm, among others. He received his bachelor’s and master’s degrees in computer science from Instituto Tecnologico y de Estudios Superiores de Monterrey, Campus Ciudad de Mexico. Peter Späth graduated in 2002 as a physicist and soon afterward became an IT consultant, mainly for Java-related projects. In 2016, he decided to concentrate on writing books on various aspects, but with the main focus on software development. With two books about graphics and sound processing, three books on Android app development, and several books on Java, Kotlin, Jakarta EE, and Spring development, Peter continues his effort in writing software development-related literature.
📄 Page
15
xvii About the Technical Reviewer Massimo Nardone has more than 27 years of experience in security, web/mobile development, and cloud and IT architecture. His true IT passions are security and Android. He has been programming and teaching others how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 27 years. He holds a master’s in computing science from the University of Salerno, Italy. He has worked as a chief information security officer (CISO), software engineer, chief security architect, security executive, OT/IoT/IIoT security leader, and architect for many years.
📄 Page
16
xix Introduction This book is a transcription to the Kotlin language. Genuine Kotlin programming techniques have been used extensively and the code has been tested thoroughly. This way, even though the Kotlin constructs are not explained in great detail, you learn about Kotlin’s expressiveness, learn how to use Kotlin for Spring Boot development, and improve your Kotlin and Spring programming skills, all at the same time.
📄 Page
17
PART I Introduction
📄 Page
18
3 © Peter Späth, Felipe Gutierrez 2025 P. Späth and F. Gutierrez, Pro Spring Boot 3 with Kotlin, https://doi.org/10.1007/979-8-8688-1131-9_1 CHAPTER 1 Spring Boot Quick Start Felipe Gutierreza* a 4109 Rillcrest Grove Way Fuquay Varina, NC 27526-3562, Albuquerque, NM, USA Project: Users App The project that you are going to learn how to build, named Users App, will expose a simple CRUD (create, read, update, and delete) API over the web. Here are the requirements for this 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. – If you are Windows user, you can use Chocolatey (https://choco- latey.org/).
📄 Page
19
4 • 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. Here are some of its 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, or 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 Spring Initializr home page, as shown in Figure 1-1. Chapter 1 Spring Boot QuiCk Start
📄 Page
20
5 Figure 1-1. Spring Initializr home page The home page includes radio buttons to select Project, Language, and Spring Boot. Project metadata includes group, artifact, name, description, package name, packaging, and Java version. Three buttons (Generate, Explore, and Share) are provided at the bottom. 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, Spring Boot 3.2.3). Chapter 1 Spring Boot QuiCk Start