Spring Boot in Practice

Book description

Go beyond the basics with Spring Boot! This practical guide presents dozens of relevant scenarios in a convenient problem-solution-discussion format.

In Spring Boot in Practice you will learn:

  • Spring Boot’s features from an expert’s perspective
  • Configuring, logging, and monitoring Spring Boot applications
  • Effective methods for database communication
  • Utilizing Spring Security and securing your Spring application in production
  • Designing and developing microservices and RESTful APIs with Spring Boot
  • Microservice versioning, documentation, and security
  • Reactive application development and reactive data access with WebSocket and RSocket
  • Deploying Spring Boot applications on Kubernetes and major cloud platforms
  • Implementing containerization in a Spring Boot application
  • Using Spring Boot with Kotlin and GraalVM

Spring Boot in Practice is full of practical recipes for common development problems in Spring Boot. Author Somnath Musib has spent years building applications with Spring, and he shares that extensive experience in this focused guide. You’ll master techniques for using Spring Data, Spring Security, and other Spring-centric solutions. Learn how to work with Spring Boot and Kotlin, handling connections for multiple platforms, and how Spring Boot can simplify building microservices and APIs. Each recipe is built around a real-world problem, complete with a full solution and thoughtful discussion.

About the Technology
With Spring Boot, it’s a snap to create standalone Spring applications that require minimal manual setup. Spring Boot directly embeds a server like Tomcat or Jetty into your project and preconfigures core Spring settings, third-party libraries, security, and other key elements. It’s a big framework, with lots of powerful features. This book provides a rich collection of techniques to help you get the most out of Spring Boot.

About the Book
Spring Boot in Practice is a cookbook-style guide to Spring application development. Following a convenient Problem-Solution-Discussion format, it takes you technique-by-technique through Spring Boot fundamentals. You’ll dive deep into auto-configuration, security, microservices, and more. Along the way, you’ll also discover numerous advanced and hidden features. All the book’s source code is open source, so you can integrate the detailed samples into your own projects.

What's Inside
  • Instantly useful techniques with reusable source code
  • Configuring, logging, and monitoring Spring Boot applications
  • Effective methods for database communication
  • Securing Spring applications in production
  • Microservices and RESTful APIs


About the Reader
For Spring Boot beginners with some Spring experience.

About the Author
Somnath Musib has over a decade of development experience, and has been actively working with Spring Boot since 2015.

Quotes
Your reliable guide to Spring…Somnath Musib does a great job, and his expert guidance makes it easy to focus on the journey that matters: the journey to production.
- Josh Long, Spring Developer Advocate, Tanzu, a division of VMWare

A–Z guide for Spring Boot.
- Najeeb Arif, Thoughtworks

Hands-on recipes and best practices that help you build applications faster.
- João Miguel Pires Dias, Mercedes-Benz.io

Invaluable for creating robust API frameworks rapidly and leveraging microservice best practices.
- Lachman Dhalliwal, Netcompany

Table of contents

  1. Spring Boot in Practice
  2. Copyright
  3. dedication
  4. brief contents
  5. contents
  6. front matter
    1. foreword
      1. We might be on to something
    2. preface
    3. acknowledgments
    4. about this book
      1. Who should read this book?
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    5. about the author
    6. about the cover illustration
  7. Part 1.
  8. 1 Booting Spring Boot
    1. 1.1 Introducing Spring Boot
      1. 1.1.1 Why Spring Boot?
      2. 1.1.2 What is Spring Boot?
      3. 1.1.3 Spring Boot core features
      4. 1.1.4 Spring Boot components
    2. 1.2 Code examples
      1. 1.2.1 Maven vs. Gradle
      2. 1.2.2 Java vs. Kotlin
      3. 1.2.3 Database support
      4. 1.2.4 Lombok
    3. 1.3 Getting started with Spring Boot
      1. 1.3.1 Your first Spring Boot project
      2. 1.3.2 Spring Boot project structure
      3. 1.3.3 Creating an executable JAR file
      4. 1.3.4 Exploring the JAR file
      5. 1.3.5 Shutting down a Spring Boot application
    4. 1.4 Spring Boot additional concepts
      1. 1.4.1 Spring Boot startup events
      2. 1.4.2 Listening events in a Spring Boot application
      3. 1.4.3 Custom Spring Boot starters
      4. 1.4.4 Custom autoconfiguration
      5. 1.4.5 Failure analyzers
      6. 1.4.6 Spring Boot actuator
      7. 1.4.7 Spring Boot developer tool
    5. Summary
  9. Part 2.
  10. 2 Common Spring Boot tasks
    1. 2.1 Managing configurations
      1. 2.1.1 Using the SpringApplication class
      2. 2.1.2 Using @PropertySource
      3. 2.1.3 Config data file
      4. 2.1.4 OS environment variable
    2. 2.2 Creating custom properties with @ConfigurationProperties
      1. 2.2.1 Technique: Defining custom properties with @ConfigurationProperties in a Spring Boot application
    3. 2.3 Executing code on Spring Boot application startup
      1. 2.3.1 Technique: Using CommandLineRunner to execute code at Spring Boot application startup
    4. 2.4 Customizing logging in a Spring Boot application
      1. 2.4.1 Technique: Understanding and customizing default Spring Boot logging in a Spring Boot application
      2. 2.4.2 Technique: Using Log4j2 to configure logging in a Spring Boot application
    5. 2.5 Validate user data using Bean Validation
      1. 2.5.1 Technique: Using built-in Bean Validation annotations to validate business entity in a Spring Boot application
      2. 2.5.2 Technique: Defining and using custom Bean Validation annotation to validate a POJO in a Spring Boot application
    6. Summary
  11. 3 Database access with Spring Data
    1. 3.1 Introducing Spring Data
      1. 3.1.1 Why Spring Data?
      2. 3.1.2 Spring Data modules
    2. 3.2 Configuring a database in a Spring Boot application
      1. 3.2.1 Technique: Configuring a relational database in a Spring Boot application
      2. 3.2.2 Technique: Configuring MongoDB database in a Spring Boot application
      3. 3.2.3 Technique: Initializing a relational database schema with a Spring Boot application
    3. 3.3 Understanding the CrudRepository interface
      1. 3.3.1 Technique: Managing domain objects in a relational database with Spring Data JPA
      2. 3.3.2 Technique: Creating a custom Spring Data repository with Spring Data JPA to manage domain objects in a relational database
    4. 3.4 Retrieve data from a database using Spring Data
      1. 3.4.1 Defining query methods
      2. 3.4.2 Technique: Defining custom query methods to retrieve domain objects from a relational database with Spring Data JPA
      3. 3.4.3 Implementing pagination with PagingAndSortingRepository
      4. 3.4.4 Technique: Using PagingAndSortingRepository interface to paginate and sort the data
      5. 3.4.5 Specifying query using @NamedQuery
      6. 3.4.6 Technique: Using a named query to manage domain objects in a relational database with Spring Data JPA
    5. 3.5 Specifying query using @Query
      1. 3.5.1 Technique: Using @Query annotation to define queries and retrieve domain objects in a relational database with Spring Data JPA
    6. 3.6 Using Criteria API with Spring Data JPA
      1. 3.6.1 Technique: Using Criteria API to manage domain objects in a relational database with Spring Data JPA
    7. 3.7 Using QueryDSL with Spring Data JPA
      1. 3.7.1 Technique: Using QueryDSL to manage domain objects in a relational database with Spring Data JPA
    8. 3.8 Managing domain object relationships
      1. 3.8.1 Technique: Managing domain objects with many-to-many relationships in a relational database with Spring Data JPA
    9. Summary
  12. 4 Spring Boot: Autoconfiguration and Actuator
    1. 4.1 Understanding Spring Boot autoconfiguration
      1. 4.1.1 Understanding @Conditional annotation
      2. 4.1.2 Deep dive into autoconfiguration
    2. 4.2 Using Spring Boot DevTools
      1. 4.2.1 Property defaults
      2. 4.2.2 Automatic restart
      3. 4.2.3 Live reload
    3. 4.3 Creating a custom failure analyzer
      1. 4.3.1 Technique: Creating a custom Spring Boot FailureAnalyzer
    4. 4.4 Spring Boot Actuator
      1. 4.4.1 Technique: Configuring Spring Boot Actuator in a Spring Boot application
      2. 4.4.2 Understanding Spring Boot Actuator endpoints
      3. 4.4.3 Managing Spring Boot Actuator endpoints
      4. 4.4.4 Health endpoint deep dive
      5. 4.4.5 Creating a custom Spring Boot HealthIndicator
      6. 4.4.6 Technique: Defining a custom Spring Boot actuator HealthIndicator
    5. 4.5 Info endpoint deep dive
      1. 4.5.1 Technique: Configuring info Spring Boot Actuator endpoint
      2. 4.5.2 Technique: Configuring a custom info contributor to provide custom application info in the Spring Boot Actuator endpoint
    6. 4.6 Creating a custom Spring Boot Actuator endpoint
      1. 4.6.1 Technique: Creating a custom Spring Boot actuator endpoint
      2. 4.6.2 Spring Boot actuator metrics
      3. 4.6.3 Creating custom metrics
      4. 4.6.4 Gauge
      5. 4.6.5 Metrics dashboard with Prometheus and Grafana
    7. Summary
  13. 5 Securing Spring Boot applications
    1. 5.1 Introducing Spring Security
    2. 5.2 Hello Spring Security with Spring Boot
      1. 5.2.1 Technique: Enabling application security with Spring Security in a Spring Boot application
      2. 5.2.2 Filter, FilterChain, and Spring Security
      3. 5.2.3 Spring Security architecture
      4. 5.2.4 Authenticating a user
      5. 5.2.5 Spring Security autoconfiguration
    3. 5.3 Using Spring Security
      1. 5.3.1 Technique: Customizing the default Spring Security login page of a Spring Boot application
      2. 5.3.2 Technique: Configuring in-memory authentication with custom users in Spring Security in a Spring Boot application
      3. 5.3.3 Technique: Configuring JDBC authentication with Spring Security in a Spring Boot application
      4. 5.3.4 Technique: Implementing JDBC authentication with custom UserDetailsService in a Spring Boot application
      5. 5.3.5 Technique: Implementing LDAP authentication in a Spring Boot application
      6. 5.3.6 Technique: Implementing HTTP basic authentication in a Spring Boot application
    4. Summary
  14. 6 Implementing additional security with Spring Security
    1. 6.1 Enabling HTTPS in a Spring Boot application
      1. 6.1.1 Technique: Enabling HTTPS in a Spring Boot application
    2. 6.2 Securing secrets in Spring Cloud Vault
      1. 6.2.1 Technique: Managing application secrets with HashiCorp Vault in a Spring Boot application
    3. 6.3 Implementing user registration
      1. 6.3.1 Technique: Implementing user registration with Spring Security in a Spring Boot application
    4. 6.4 Implementing email verification at user registration
      1. 6.4.1 Technique: Validating user email addresses in a Spring Boot application
    5. 6.5 Controlling multiple incorrect login attempts
      1. 6.5.1 Technique: Controlling multiple incorrect login attempts in a Spring Boot application
    6. 6.6 Implementing a Remember Me feature
      1. 6.6.1 Technique: Enabling a Remember Me feature in a Spring Boot application with Spring Security
    7. 6.7 Implementing reCAPTCHA
      1. 6.7.1 Technique: Enabling Google reCAPTCHA in a Spring Boot application with Spring Security
    8. 6.8 Enabling two-factor authentication with Google Authenticator
      1. 6.8.1 Technique: Enabling two-factor authentication in a Spring Boot application
    9. 6.9 Authentication with OAuth2
      1. 6.9.1 Technique: Enabling sign in with Google in a Spring Boot application
    10. 6.10 Securing Actuator endpoints
      1. 6.10.1 Technique: Securing Spring Boot Actuator endpoints
    11. Summary
  15. 7 Developing RESTful Web services with Spring Boot
    1. 7.1 Developing a RESTful API with Spring Boot
      1. 7.1.1 Technique: Developing a RESTful API using Spring Boot
    2. 7.2 Managing exceptions in a Spring Boot RESTful API
      1. 7.2.1 Technique: Handling exceptions in a RESTful API
    3. 7.3 Testing a RESTful API
      1. 7.3.1 Technique: Testing a RESTful API in a Spring Boot application
    4. 7.4 Documenting a RESTful API
      1. 7.4.1 Technique: Documenting a RESTful API with OpenAPI
    5. 7.5 Implementing RESTful API versioning
      1. 7.5.1 Technique: Implementing versioning in a RESTful API
    6. 7.6 Securing a RESTful API
      1. 7.6.1 Technique: Using JWT to authorize RESTful API requests
    7. Summary
  16. Part 3.
  17. 8 Reactive Spring Boot application development
    1. 8.1 Introduction to reactive programming
      1. 8.1.1 Backpressure
      2. 8.1.2 Benefits of reactive programming
    2. 8.2 Understanding Project Reactor
    3. 8.3 Introducing Spring WebFlux
      1. 8.3.1 Technique: Developing a reactive RESTful API with annotated controllers
      2. 8.3.2 Technique: Developing a reactive RESTful API with functional endpoints
    4. 8.4 Testing reactive applications
      1. 8.4.1 Technique: Using WebClient to build an API client
    5. 8.5 Introduction to RSocket
      1. 8.5.1 Technique: Developing applications using RSocket and Spring Boot
    6. 8.6 Introduction to WebSocket
      1. 8.6.1 Technique: Developing an application using WebSocket and Spring Boot
    7. Summary
  18. Part 4.
  19. 9 Deploying Spring Boot applications
    1. 9.1 Running Spring Boot applications as executable JAR files
      1. 9.1.1 Technique: Packaging and executing a Spring Boot application as an executable JAR file
    2. 9.2 Deploying Spring Boot applications as WAR in the WildFly application server
      1. 9.2.1 Technique: Packaging and deploying a Spring Boot application as WAR in the WildFly application server
    3. 9.3 Deploying Spring Boot applications in Cloud Foundry
      1. 9.3.1 Technique: Deploying a Spring Boot application to Cloud Foundry
    4. 9.4 Deploying Spring Boot applications in Heroku
      1. 9.4.1 Technique: Deploying a Spring Boot application in Heroku
    5. 9.5 Running Spring Boot applications as Docker containers
      1. 9.5.1 Technique: Creating a container image and running a Spring Boot application as a container
    6. 9.6 Deploying Spring Boot applications in a Kubernetes cluster
      1. 9.6.1 Technique: Deploying a Spring Boot application in a Kubernetes cluster
    7. 9.7 Deploying Spring Boot applications in Red Hat OpenShift
      1. 9.7.1 Technique: Deploying a Spring Boot application in the Red Hat OpenShift platform
    8. Summary
  20. Part 5.
  21. 10 Spring Boot with Kotlin, Native Image, and GraphQL
    1. 10.1 Spring Boot with Kotlin
      1. 10.1.1 Technique: Developing a Spring Boot application with Kotlin
      2. 10.1.2 Technique: Securing a Spring Boot Kotlin application with Spring Security
    2. 10.2 Introducing Spring Native
      1. 10.2.1 Introduction to GraalVM
      2. 10.2.2 GraalVM native image
      3. 10.2.3 Spring Boot native image
      4. 10.2.4 Technique: Generating Spring Boot native image using buildpacks
      5. 10.2.5 Technique: Generating Spring Boot native image using a Maven plugin
      6. 10.2.6 Understanding Spring AOT Maven Plugin
    3. 10.3 Spring Boot with GraphQL
      1. 10.3.1 Issues with REST
      2. 10.3.2 Introduction to GraphQL
      3. 10.3.3 Using GraphQL with Spring Boot
      4. 10.3.4 Technique: Developing a GraphQL API with a Spring Boot application
      5. 10.3.5 Technique: Developing a GraphQL API over WebSocket with a Spring Boot application
    4. Summary
  22. Appendix A. Generating and building Spring Boot projects
    1. A.1 Generating Spring Boot applications with Spring Initializr
      1. A.1.1 Introducing Spring Initializr
      2. A.1.2 Technique: Generating a Spring Boot application with the Spring Initializr Web user interface
      3. A.1.3 Technique: Generating a Spring Boot application with Spring Initializr in IntelliJ IDEA IDE
      4. A.1.4 Technique: Generating a Spring Boot Application with Spring Initializr using the Spring Tool Suite
      5. A.1.5 Technique: Generating a Spring application with Spring Initializr in Visual Studio Code
      6. A.1.6 Technique: Generating a Spring Boot application with Spring Initializr using Command Line Interface
    2. A.2 Bootstrapping and executing a Spring Boot application with Spring Boot CLI
      1. A.2.1 Installing the Spring Boot CLI
      2. A.2.2 Technique: Developing a simple Spring Boot application with Spring Boot CLI
      3. A.2.3 Technique: Building a Web application using Spring Boot CLI
  23. Appendix B. Spring MVC and Thymeleaf Template Engine
    1. B.1 Revisiting Spring MVC
      1. B.1.1 Front controller design pattern
      2. B.1.2 Understanding request processing
    2. B.2 Understanding Thymeleaf
      1. B.2.1 Displaying attributes
      2. B.2.2 Conditional evaluation
      3. B.2.3 Managing forms
    3. B.3 Enabling a template engine in Spring Boot
      1. B.3.1 Technique: Building a Spring Boot Web application with Thymeleaf
  24. index

Product information

  • Title: Spring Boot in Practice
  • Author(s): Somnath Musib
  • Release date: August 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617298813