Spring Boot 3.0 Cookbook....2
Contributors....5
About the author....5
About the reviewers....5
Preface....29
Who this book is for....30
What this book covers....31
To get the most out of this book....32
Download the example code files....33
Conventions used....33
Sections....34
Getting ready....34
How to do it…....34
How it works…....35
There’s more…....35
See also....35
Get in touch....35
Share Your Thoughts....35
Download a free PDF copy of this book....35
Part 1:Web Applications and Microservices....37
Chapter 1: Building RESTful APIs....38
Technical requirements....38
Creating a RESTful API....39
Getting ready....39
How to do it.......39
How it works.......42
There’s more.......43
See also....44
Defining responses and the data model exposed by the API....44
Getting ready....44
How to do it.......44
How it works.......47
Managing errors in a RESTful API....48
Getting ready....48
How to do it.......49
How it works.......50
There’s more.......50
Testing a RESTful API....50
Getting ready....51
How to do it.......51
How it works.......53
There’s more.......53
See also....53
Using OpenAPI to document our RESTful API....54
Getting ready....54
How to do it.......54
How it works.......56
Consuming a RESTful API from another Spring Boot application using FeignClient....56
Getting ready....56
How to do it.......56
How it works.......58
There’s more.......59
Consuming a RESTful API from another Spring Boot application using RestClient....59
Getting ready....59
How to do it.......59
How it works.......60
Mocking a RESTful API....61
Getting ready....62
How to do it.......62
How it works.......63
See also....64
Chapter 2: Securing Spring Boot Applications with OAuth2....65
Technical requirements....65
Setting up Spring Authorization Server....66
Getting ready....66
How to do it…....66
How it works…....69
There’s more…....70
See also....70
Protecting a RESTful API using OAuth2....71
Getting ready....71
How to do it…....72
How it works…....74
Protecting a RESTful API using OAuth2 with different scopes....75
Getting ready....76
How to do it…....76
How it works…....78
There’s more…....78
See also....79
Configuring an MVC application with OpenID authentication....79
Getting ready....80
How to do it…....80
How it works…....89
See also....89
Logging in with Google Accounts....90
Getting ready....90
How to do it…....90
How it works…....101
See also....102
Integrating a RESTful API with a cloud IdP....103
Getting ready....103
How to do it…....103
How it works…....112
There’s more…....113
Chapter 3: Observability, Monitoring, and Application Management....117
Technical requirements....118
Adding Actuator to your application....118
Getting ready....119
How to do it…....119
How it works…....120
There’s more…....120
See also....120
Creating a custom Actuator endpoint....121
Getting ready....121
How to do it…....121
How it works…....124
Using probes and creating a custom health check....125
Getting ready....125
How to do it…....126
How it works…....129
See also....130
Implementing distributed tracing....130
Getting ready....131
How to do it…....131
How it works…....136
There’s more…....137
Accessing standard metrics....139
Getting ready....139
How to do it…....140
How it works…....142
See also....143
Creating your own metrics....143
Getting ready....144
How to do it…....144
How it works…....146
There’s more…....146
Integrating your application with Prometheus and Grafana....148
Getting ready....148
How to do it…....149
How it works…....156
Changing the settings of a running application....157
Getting ready....157
How to do it…....158
How it works…....160
There’s more…....160
Chapter 4: Spring Cloud....161
Technical requirements....161
Setting up Eureka Server....162
Getting ready....162
How to do it.......162
How it works.......164
Integrating an application in Eureka Server....164
Getting ready....164
How to do it.......164
How it works.......167
There’s more.......168
Scaling out the RESTful API....168
Getting ready....169
How to do it.......169
How it works.......171
Setting up Spring Cloud Gateway....171
Getting ready....173
How to do it.......173
How it works.......174
See also....175
Testing Spring Cloud Gateway....175
Getting ready....176
How to do it.......176
How it works.......178
Setting up Spring Cloud Config....178
Getting ready....179
How to do it.......179
How it works.......181
There’s more.......183
See also....183
Integrating distributed tracing with Spring Cloud....184
Getting ready....184
How to do it.......184
How it works.......187
Deploying Spring Boot Admin....187
Getting ready....188
How to do it.......188
How it works.......192
Protecting Spring Cloud Gateway....193
Getting ready....193
How to do it.......193
How it works.......196
Part 2: Database Technologies....197
Chapter 5: Data Persistence and Relational Database Integration with Spring Data....198
Technical requirements....199
Connecting your application to PostgreSQL....199
Getting ready....199
How to do it.......200
How it works.......202
There’s more.......202
Using JdbcClient to access the database....203
Getting ready....203
How to do it.......203
How it works.......204
Using an ORM to access the database....204
Getting ready....205
How to do it.......205
How it works.......208
There’s more.......209
See also....210
Creating the database schema from our code....210
Getting ready....210
How to do it.......210
How it works.......212
There’s more.......213
See also....213
PostgreSQL integration tests with Testcontainers....213
Getting ready....214
How to do it.......214
How it works.......215
Versioning and upgrading database schema....216
Getting ready....216
How to do it.......216
How it works.......219
There’s more.......220
See also....220
Using JPQL....221
Getting ready....221
How to do it.......221
How it works.......225
There’s more.......225
Using Native Queries....226
Getting ready....226
How to do it.......226
How it works.......228
There’s more.......229
See also....229
Updating Operations....230
Getting ready....230
How to do it.......230
How it works.......234
See also....235
Using Dynamic Queries....235
Getting ready....236
How to do it.......236
How it works.......238
There’s more.......238
See also....238
Using Transactions....239
Getting ready....239
How to do it.......239
How it works.......241
There’s more.......243
See also....243
Chapter 6: Data Persistence and NoSQL Database Integration with Spring Data....245
Technical requirements....246
Connecting your application to MongoDB....246
Getting ready....246
How to do it.......247
How it works.......250
There’s more.......251
Using Testcontainers with MongoDB....251
Getting ready....251
How to do it.......252
How it works.......253
Data indexing and sharding in MongoDB....253
Getting ready....254
How to do it.......254
How it works.......256
There’s more.......258
Using transactions in MongoDB....258
Getting ready....259
How to do it.......259
How it works.......261
See also....262
Deploying a MongoDB cluster in Testcontainers....262
Getting ready....262
How to do it.......262
How it works.......264
Managing concurrency with MongoDB....265
Getting ready....266
How to do it.......266
How it works.......267
Connecting your application to Apache Cassandra....268
Getting ready....268
How to do it.......268
How it works.......270
See also....271
Using Testcontainers with Cassandra....271
Getting ready....271
How to do it.......272
How it works.......273
Using Apache Cassandra templates....273
Getting ready....274
How to do it.......274
How it works.......275
There’s more.......276
Managing concurrency with Apache Cassandra....276
Getting ready....277
How to do it.......277
How it works.......278
Part 3: Application Optimization....279
Chapter 7: Finding Bottlenecks and Optimizing Your Application....280
Technical requirements....280
Tuning the database connection pool....281
Getting ready....281
How to do it…....282
How it works…....286
There’s more…....287
Caching dependencies....287
Getting ready....288
How to do it…....288
How it works…....290
There’s more…....290
Using shared cache....292
Getting ready....292
How to do it…....293
How it works…....294
Using Testcontainers with Redis cache....295
Getting ready....295
How to do it…....296
How it works…....296
Creating a native image using Spring Boot....297
Getting ready....297
How to do it…....297
How it works…....298
Using GraalVM Tracing Agent to configure the native application....299
Getting ready....299
How to do it…....300
How it works…....301
There’s more…....302
Creating a native executable using Spring Boot....303
Getting ready....303
How to do it…....304
How it works…....304
There’s more…....304
Creating a native executable from a JAR....305
Getting ready....306
How to do it…....306
How it works…....307
Chapter 8: Spring Reactive and Spring Cloud Stream....308
Technical requirements....309
Creating a reactive RESTful API....309
Getting ready....310
How to do it.......310
How it works.......311
There’s more.......312
Using a reactive API client....313
Getting ready....313
How to do it.......313
How it works.......315
Testing reactive applications....315
Getting ready....315
How to do it.......316
How it works.......318
Connecting to PostgreSQL using Spring Data R2DBC....319
Getting ready....319
How to do it.......320
How it works.......322
There’s more.......323
Event-driven applications with Spring Cloud Stream and RabbitMQ....324
Getting ready....324
How to do it.......325
How it works.......327
There’s more.......329
See also....330
Routing messages with Spring Cloud Stream and RabbitMQ....330
Getting ready....330
How to do it.......331
How it works.......332
See also....334
Error handling with Spring Cloud Stream....334
Getting ready....335
How to do it.......335
How it works.......338
See also....339
Part 4: Upgrading to Spring Boot 3 from Previous Versions....340
Chapter 9: Upgrading from Spring Boot 2.x to Spring Boot 3.0....341
Technical requirements....341
Preparing the application....342
Getting ready....342
How to do it.......343
How it works.......344
See also....345
Preparing Spring Security....345
Getting ready....345
How to do it.......345
How it works.......347
See also....347
Detecting property changes....348
Getting ready....348
How to do it.......348
How it works.......350
Upgrade the project to Spring Boot 3....350
Getting ready....350
How to do it.......351
How it works.......352
Upgrading Spring Data....353
Getting ready....353
How to do it.......353
How it works.......356
There’s more.......357
See also....357
Managing Actuator changes....357
Getting ready....357
How to do it.......358
How it works.......360
See also....361
Managing web application changes....361
Getting ready....362
How to do it.......362
How it works.......362
There’s more.......362
See also....363
Using OpenRewrite for migration automation....363
Getting ready....363
How to do it.......363
How it works.......367
There’s more.......367
See also....368
Index....369
Why subscribe?....385
Other Books You May Enjoy....385
Packt is searching for authors like you....389
Download a free PDF copy of this book....390
In today's dynamic landscape, crafting robust and scalable Java web applications presents formidable challenges. Spring Boot emerges as the leading framework for web and microservices development, featuring a dynamic ecosystem and seamless integrations to address a spectrum of scenarios, from scaling apps on the cloud to deploying them to production. In this book, you’ll explore its streamlined, convention-over-configuration approach, simplifying application development.
You’ll start by covering recipes showcasing Spring Boot's features. As you progress, you’ll understand how it helps streamline application development while staying ahead of technology trends. The book helps you grasp concepts effectively, explores basic REST APIs, shows you how to escalate to advanced scenarios, and tackle common cloud application challenges like security, scalability, performance optimization, and automated deployments. Dedicated sections are designed to help you stay ahead of the curve with recipes that delve into the latest trends such as containers, observability, native images, DevOps, test automation, and microservices, ensuring your applications align with evolving industry standards.
By the end of this book, you’ll be able to build and automate the deployment of a scalable and high-performing distributed solution using Spring Boot 3.
This book is for Java developers who want to gain expertise in modern web development, architects designing complex systems, experienced Spring Boot developers and technology enthusiasts looking to stay up to date with the latest trends, and software engineers in need of practical solutions for everyday challenges. Hands-on experience with Java or Kotlin is required. Prior development experience on the cloud will be useful, but not necessary.