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
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
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
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
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
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
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
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
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
Quickly and productively build complex Kotlin-based Spring applications and microservices out of the box, with minimal concern over things such as configurations. This revised edition will show you how to fully leverage the Spring Boot 3 micro-framework and apply it to create enterprise-ready Kotlin-based cloud-native applications, microservices, and web applications that just work.
The book covers what has been added to the new Spring Boot 3 release, including improved support for the Kotlin programming language, changes to Stream Editor UI, Maven Preemptive Authentication, building Docker images using cloud-native build packs, building layered jars for optimized Docker images, E2E traceability for configuration properties, many dependency upgrades, support for Spring Data, and much more.
This book is your in-depth pragmatic guide for increasing your enterprise Kotlin and cloud application productivity while decreasing development time. It is a no-nonsense guide with case studies of increasing complexity throughout the book. The main author, a senior solutions architect and Principal Technical instructor at Pivotal, the company behind the Spring Framework, shares his experience, insights, and first-hand knowledge about how Spring Boot technology works, and best practices.
This is an essential book for your Kotlin-based Spring learning and reference library.
Build cloud-native apps and microservices with the Spring Boot 3 framework
Persist and access your data using and integrating with Spring Data
Message with Kafka, RabbitMQ, and WebSockets
Explore Spring Cloud projects
Extend Spring Boot by creating your own Spring Boot Starter and @Enable feature
Test and deploy Spring Boot with best practices
Effectively use Kotlin as a programming language for Spring applications
Experienced Spring, Java, and Kotlin developers seeking increased productivity gains and decreased complexity and development time in their applications and software services