Developing Java Applications with Spring and Spring Boot

Book description

An end-to-end software development guide for the Java eco-system using the most advanced frameworks: Spring and Spring Boot. Learn the complete workflow by building projects and solving problems.

About This Book
  • Learn reactive programming by implementing a reactive application with Spring WebFlux
  • Create a robust and scalable messaging application with Spring messaging support
  • Get up-to-date with the defining characteristics of Spring Boot 2.0 in Spring Framework 5
  • Learn about developer tools, AMQP messaging, WebSockets, security, MongoDB data access, REST, and more
  • This collection of effective recipes serves as guidelines for Spring Boot application development
Who This Book Is For

Java developers wanting to build production-grade applications using the newest popular Spring tools for a rich end-to-end application development experience.

What You Will Learn
  • Get to know the Spring Boot and understand how it makes creating robust applications extremely simple
  • Understand how Spring Data helps us add persistence in MongoDB and SQL databases
  • Implement a websocket to add interactive behaviors in your applications
  • Create powerful, production-grade applications and services with minimal fuss
  • Use custom metrics to track the number of messages published and consumed
  • Build anything from lightweight unit tests to fully running embedded web container integration tests
  • Learn effective testing techniques by integrating Cucumber and Spock
  • Use Hashicorp Consul and Netflix Eureka for dynamic Service Discovery
In Detail

Spring Framework has become the most popular framework for Java development. It not only simplifies software development but also improves developer productivity. This book covers effective ways to develop robust applications in Java using Spring.

The course is up made of three modules, each one having a take-away relating to building end-to-end java applications. The first module takes the approach of learning Spring frameworks by building applications.You will learn to build APIs and integrate them with popular fraemworks suh as AngularJS, Spring WebFlux, and Spring Data. You will also learn to build microservices using Spring's support for Kotlin. You will learn about the Reactive paradigm in the Spring architecture using Project Reactor. In the second module, after getting hands-on with Spring, you will learn about the most popular tool in the Spring ecosystem-Spring Boot. You will learn to build applications with Spring Boot, bundle them, and deploy them on the cloud. After learning to build applications with Spring Boot, you will be able to use various tests that are an important part of application development. We also cover the important developer tools such as AMQP messaging, websockets, security, and more. This will give you a good functional understanding of scalable development in the Spring ecosystem with Spring Boot. In the third and final module, you will tackle the most important challenges in Java application development with Spring Boot using practical recipes. Including recipes for testing, deployment, monitoring, and securing your applications. This module will also address the functional and technical requirements for building enterprise applications.

By the end of the course you will be comfortable with using Spring and Spring Boot to develop Java applications and will have mastered the intricacies of production-grade applications.

Style and approach

A simple step-by-step guide with practical examples to help you develop and deploy Spring and Spring Boot applications in the real-world.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Publisher resources

View/Submit Errata

Table of contents

  1. Title Page - Courses
  2. Copyright and Credits - Courses
    1. Developing Java Applications with Spring and Spring Boot
  3. Packt Upsell - Courses
    1. Why subscribe?
    2. PacktPub.com
  4. Preface
    1. Who this learning path is for
    2. What this learning path covers
    3. To get the most out of this learning path
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  5. Spring 5.0 By Example
  6. Journey to the Spring World
    1. Spring modularity
      1. Spring Core Framework
        1. Core container
        2. Spring Messaging
          1. Spring AMQP
          2. Spring for Apache Kafka
          3. Spring JMS
        3. Spring Web MVC
        4. Spring WebFlux
        5. Spring Data
        6. Spring Security
      2. Spring Cloud
        1. Spring Cloud Netflix
        2. Spring Cloud Config
        3. Spring Cloud Consul
        4. Spring Cloud Security
        5. Spring Cloud Bus
        6. Spring Cloud Stream
        7. Spring Integration
      3. Spring Boot
    2. Setting up our development environment
      1. Installing OpenJDK
      2. Installing Maven
      3. Installing IDE
        1. IntelliJ IDEA
        2. Spring Tools Suite
        3. Installing Docker
      4. Introducing Docker concepts
        1. Docker images
        2. Containers
        3. Docker networks
        4. Docker volumes
        5. Docker commands
          1. Docker run
          2. Docker container
          3. Docker network
          4. Docker volume
    3. Summary
  7. Starting in the Spring World – the CMS Application
    1. Creating the CMS application structure
      1. The CMS project
        1. Project metadata section
        2. The dependencies section
        3. Generating the project
    2. Running the application
      1. Looking under the hood
      2. Running the application
        1. IntelliJ IDEA
        2. Command line
          1. Command line via the Maven goal
          2. Command line via the JAR file
    3. Creating the REST resources
      1. Models
        1. Adding Lombok dependency
        2. Creating the models
          1. Tag
          2. Category
          3. User
          4. News
      2. Hello REST resources
        1. Creating the CategoryResource class
        2. UserResource
        3. NewsResource
    4. Adding service layer
      1. Changes in the model
        1. Adding a new review
        2. Keeping the news safely
      2. Before starting the service layer
        1. CategoryService
        2. UserService
        3. NewsService
      3. Configuring Swagger for our APIs
        1. Adding dependencies to pom.xml
        2. Configuring Swagger
        3. First documented API
    5. Integrate with AngularJS
      1. AngularJS concepts
        1. Controllers
        2. Services
        3. Creating the application entry point
        4. Creating the Category Controller
        5. Creating the Category Service
    6. Summary
  8. Persistence with Spring Data and Reactive Fashion
    1. Learning the basics of Docker
      1. Preparing  MongoDB
      2. Preparing a PostgreSQL database
    2. Spring Data project
      1. Spring Data JPA
        1. Configuring pom.xml for Spring Data JPA
        2. Configuring the Postgres connections
        3. Mapping the models
        4. Adding the JPA repositories in the CMS application
        5. Configuring transactions
        6. Installing and configuring pgAdmin3
        7. Checking the data on the database structure
    3. Creating the final data access layer
      1. Spring Data MongoDB
        1. Removing the PostgreSQL and Spring Data JPA dependencies
        2. Mapping the domain model
        3. Configuring the database connection
        4. Adding the repository layer
        5. Checking the persistence
    4. Creating the Docker image for CMS
      1. Configuring the docker-maven-plugin
        1. Adding the plugin on pom.xml
        2. Pushing the image to Docker Hub
        3. Configuring the Docker Spring profile
        4. Running the Dockerized CMS
    5. Putting in Reactive fashion
      1. Reactive Spring
        1. Project Reactor
          1. Components
          2. Hot and cold
          3. Reactive types
          4. Let's play with the Reactor
      2. Spring WebFlux
        1. Event-loop model
      3. Spring Data for Reactive Extensions
        1. Spring Data Reactive
        2. Reactive repositories in practice
        3. Creating the first Reactive repository
      4. Fixing the service layer
        1. Changing the CategoryService
      5. Changing the REST layer
        1. Adding the Spring WebFlux dependency
        2. Changing the CategoryResource
    6. Summary
  9. Kotlin Basics and Spring Data Redis
    1. Learning Kotlin basics
      1. Main characteristics of Kotlin
        1. Syntax
      2. Semantics
        1. Declaring functions in Kotlin
          1. Simple function with parameters and return type
          2. Simple function without return
          3. Single expressions functions
          4. Overriding a function
        2. Data classes
        3. Objects
        4. Companion objects
      3. Kotlin idioms
        1. String interpolation
        2. Smart Casts
        3. Range expressions
          1. Simple case
          2. The until case
          3. The downTo case
          4. Step case
        4. Null safety
          1. Safe calls
          2. Elvis operator
    2. Wrapping it up
    3. Creating the project
      1. Project use case
      2. Creating the project with Spring Initializr
        1. Adding Jackson for Kotlin
      3. Looking for the Maven plugins for Kotlin
      4. Creating a Docker network for our application
      5. Pulling the Redis image from the Docker Hub
    4. Running the Redis instance
      1. Configuring the redis-cli  tool
    5. Understanding Redis
      1. Data types
        1. Strings
          1. Main commands
        2. Lists
          1. Main commands
        3. Sets
          1. Main commands
    6. Spring Data Reactive Redis
      1. Configuring the ReactiveRedisConnectionFactory
      2. Providing a ReactiveRedisTemplate
      3. Creating Tracked Hashtag repository
        1. Creating the service layer
      4. Exposing the REST resources
    7. Creating a Twitter application
      1. Configuring pom.xml
      2. Creating the image
      3. Running the container
    8. Testing APIs
    9. Summary
  10. Reactive Web Clients
    1. Creating the Twitter Gathering project
      1. Project structure
    2. Starting the RabbitMQ server with Docker
      1. Pulling the RabbitMQ image from Docker Hub
      2. Starting the RabbitMQ server
    3. Spring Messaging AMQP
      1. Adding Spring AMQP in our pom.xml
      2. Integrating Spring Application and RabbitMQ
      3. Understanding RabbitMQ exchanges, queues, and bindings
        1. Exchanges
          1. Direct exchanges
          2. Fanout exchanges
          3. Topic exchanges
          4. Header exchanges
        2. Queues
        3. Bindings
      4. Configuring exchanges, queues, and bindings on Spring AMQP
        1. Declaring exchanges, queues, and bindings in yaml
        2. Declaring Spring beans for RabbitMQ
      5. Consuming messages with Spring Messaging
      6. Producing messages with Spring Messaging
    4. Enabling Twitter in our application
      1. Producing Twitter credentials
        1. Configuring Twitter credentials in application.yaml
        2. Modelling objects to represent Twitter settings
          1. Twittertoken
          2. TwitterAppSettings
        3. Declaring Twitter credentials for the Spring container
    5. Spring reactive web clients
      1. Producing WebClient in a Spring Way
      2. Creating the models to gather Tweets
      3. Authentication with Twitter APIs
      4. Some words about server-sent events (SSE)
      5. Creating the gather service
      6. Listening to the Rabbit Queue and consuming the Twitter API
    6. Changing the Tracked Hashtag Service
      1. Adding the Spring Starter RabbitMQ dependency
      2. Configuring the RabbitMQ connections
      3. Creating exchanges, queues, and bindings for the Twitter Hashtag Service
      4. Sending the messages to the broker
    7. Testing the microservice's integrations
      1. Running Tracked Hashtag Service
      2. Running the Twitter Gathering
      3. Testing stuff
    8. Spring Actuator
      1. Adding Spring Boot Actuator in our pom.xml
      2. Actuator Endpoints
      3. Application custom information
      4. Testing endpoints
    9. Summary
  11. Playing with Server-Sent Events
    1. Creating the Tweet Dispatcher project
      1. Using Spring Initializr once again
      2. Additional dependencies
    2. Server-Sent Events
      1. A few words about the HTTP protocol
      2. HTTP and persistent connections
        1. WebSockets
        2. Server-Sent Events
    3. Reactor RabbitMQ
      1. Understanding the Reactor RabbitMQ
      2. Configuring RabbitMQ Reactor beans
      3. Consuming the RabbitMQ queues reactively
    4. Filtering streams
    5. Dockerizing the whole solution
      1. Tweet Gathering
      2. Tweet Dispatcher
    6. Running the containerized solution
      1. Running the Tracked Hashtag Service container
      2. Running the Tweet Gathering container
      3. Running the Tweet Dispatcher container
    7. The docker-compose tool
      1. Installing docker-compose
      2. Creating a docker-compose file
      3. Running the solution
      4. Testing the network
    8. Summary
  12. Airline Ticket System
    1. The Airline Ticket System
      1. Airline functionalities
      2. Solution diagram
    2. Spring Cloud Config Server
      1. Creating the Config Server project
      2. Enabling Spring Cloud Config Server
        1. Using GitHub as a repository
        2. Configuring the Spring Boot application
        3. Configuring the Git repository as a properties source
        4. Running the Config Server
        5. Testing our Config Server
    3. Spring Cloud service discovery
      1. Creating Spring Cloud Eureka
      2. Creating the Eureka server main class
      3. Configuring the Spring Cloud Eureka server
      4. Running the Spring Cloud Eureka server
    4. Spring Cloud Zipkin server and Sleuth
      1. Infrastructure for the Zipkin server
      2. Creating the Spring Cloud Zipkin server
      3. Configuring boostrap.yaml and application.yaml
      4. Running the Zipkin server
    5. Spring Cloud Gateway
      1. Creating the Spring Cloud Gateway project
      2. Creating the Spring Cloud Gateway main class
      3. Configuring the Spring Cloud Gateway project
      4. Running the Spring Cloud Gateway
      5. Checking the Eureka server
      6. Creating our first route with Spring Cloud Gateway
    6. Putting the infrastructure on Docker
    7. Summary
  13. Circuit Breakers and Security
    1. Understanding the service discovery power
      1. Creating the planes microservice
      2. Coding the planes microservice
      3. The reactive repository
      4. Creating the Plane service
      5. The REST layer
      6. Running the plane microservice
    2. Flights microservice
      1. Cloning the Flight microservice project
      2. Netflix Ribbon
      3. Understanding the discovery client
      4. Service discovery and load balancing in practice
    3. When the services fail, hello Hystrix
      1. Hystrix in a nutshell
      2. Spring Cloud Hystrix
    4. Spring Boot Admin
      1. Running Spring Boot Admin
    5. Spring Cloud Zuul
      1. Understanding the EDGE service project
      2. Creating the EDGE server
    6. Summary
  14. Putting It All Together
    1. The airline Bookings microservice
    2. The airline Payments microservice
    3. Learning about the Turbine server
      1. Creating the Turbine server microservice
    4. Hystrix Dashboard
    5. Creating the Mail microservice
      1. Creating the SendGrid account
      2. Creating the Mail microservice project
        1. Adding RabbitMQ dependencies
        2. Configuring some RabbitMQ stuff
        3. Modeling a Mail message
        4. The MailSender class
        5. Creating the RabbitMQ queue listener
        6. Running the Mail microservice
    6. Creating the Authentication microservice
      1. Creating the Auth microservice
      2. Configuring the security
      3. Testing the Auth microservice
        1. Client credentials flow
        2. Implicit grant flow
      4. Protecting the microservices with OAuth 2.0
        1. Adding the security dependency
        2. Configuring the application.yaml file
        3. Creating the JwtTokenStore Bean
    7. Monitoring the microservices
      1. Collecting metrics with Zipkin
      2. Collection commands statistics with Hystrix
    8. Dockerizing the microservices
    9. Running the system
    10. Summary
  15. Learning Spring Boot 2.0
  16. Quick Start with Java
    1. Getting started
    2. Spring Boot starters
    3. Running a Spring Boot application
    4. Delving into Spring Boot's property support
    5. Bundling up the application as a runnable JAR file
    6. Deploying to Cloud Foundry
    7. Adding production-ready support
      1. Pinging our app for general health
      2. Metrics
    8. Summary
  17. Reactive Web with Spring Boot
    1. Creating a reactive web application with Spring Initializr
    2. Learning the tenets of reactive programming
    3. Introducing Reactor types
    4. Switching from Embedded Netty to Apache Tomcat
    5. Comparing reactive Spring WebFlux against classic Spring MVC
      1. Why is Spring doing this?
    6. Showing some Mono/Flux-based endpoints
    7. Creating a reactive ImageService
    8. Creating a reactive file controller
    9. Why use reactive programming?
    10. Interacting with a Thymeleaf template
    11. Illustrating how going from async to sync can be easy, but the opposite is not
    12. Summary
  18. Reactive Data Access with Spring Boot
    1. Getting underway with a reactive data store
    2. Solving a problem
      1. Wiring up Spring Data repositories with Spring Boot
    3. Creating a reactive repository
    4. Pulling data through a Mono/Flux and chain of operations
    5. Creating custom finders
    6. Querying by example
    7. Querying with MongoOperations
    8. Logging reactive operations
    9. Summary
  19. Testing with Spring Boot
    1. Test dependencies
    2. Unit testing
    3. Slice-based testing
      1. Testing with embedded MongoDB
      2. Testing with a real MongoDB database
    4. Testing WebFlux controllers
    5. Fully embedded Spring Boot app tests
    6. Testing your custom Spring Boot autoconfiguration
    7. Summary
  20. Developer Tools for Spring Boot Apps
    1. Using Spring Boot's DevTools for hot code reloading
    2. Using Spring Boot's autoconfiguration report
    3. Making local changes and seeing them on the target system
    4. Writing a custom health check
    5. Adding build data to /application/info
    6. Creating custom metrics
    7. Working with additional Actuator endpoints
    8. Summary
  21. AMQP Messaging with Spring Boot
    1. Getting started with RabbitMQ
      1. Installing RabbitMQ broker
      2. Launching the RabbitMQ broker
    2. Adding messaging as a new component to an existing application
    3. Creating a message producer/message consumer
      1. Displaying comments
      2. Producing comments
      3. AMQP fundamentals
    4. Adding customized metrics to track message flow
    5. Peeking at Spring Cloud Stream (with RabbitMQ)
      1. Introduction to Spring Cloud
      2. Logging with Spring Cloud Stream
    6. Summary
  22. Microservices with Spring Boot
    1. A quick primer on microservices
    2. Dynamically registering and finding services with Eureka
    3. Introducing @SpringCloudApplication
    4. Calling one microservice from another with client-side load balancing
    5. Implementing microservice circuit breakers
    6. Monitoring circuits
    7. Offloading microservice settings to a configuration server
    8. Summary
  23. WebSockets with Spring Boot
    1. Publishing saved comments to a chat service
    2. Creating a chat service to handle WebSocket traffic
      1. Brokering WebSocket messages
      2. Broadcasting saved comments
      3. Configuring WebSocket handlers
    3. Consuming WebSocket messages from the web page
    4. Moving to a fully asynchronous web client
    5. Handling AJAX calls on the server
    6. Introducing user chatting
    7. Sending user-specific messages
      1. Registering users without authentication
      2. Linking a user to a session
      3. Sending user-to-user messages
    8. Checking out the final product
    9. JSR 356 versus Spring WebFlux messaging
    10. Summary
  24. Securing Your App with Spring Boot
    1. Securing a Spring Boot application
    2. Using Spring Session
      1. Creating a Gateway API
    3. Securing the chat microservice
      1. Authentication versus authorization
    4. Sharing session details with other microservices
    5. Securing the images microservice
    6. Wiring in image ownership
      1. Authorizing methods
    7. Tailoring the UI with authorization checks
    8. Securing WebSockets
    9. Tracing calls
    10. Securing the Config Server
    11. Securing the Eureka Server
    12. Summary
  25. Taking Your App to Production with Spring Boot
    1. Profile-based sets of beans
    2. Creating configuration property beans
    3. Overriding property settings in production
      1. @ConfigurationProperties versus @Value
    4. Pushing app to Cloud Foundry and adjusting the settings
    5. Summary
  26. Spring Boot 2.0 Cookbook
  27. Getting Started with Spring Boot
    1. Introduction
    2. Using a Spring Boot template and starter
      1. How to do it...
      2. How it works...
    3. Creating a simple application
      1. How to do it...
      2. How it works...
    4. Launching an application using Gradle
      1. How to do it...
      2. How it works...
    5. Using the command-line runners
      1. How to do it...
      2. How it works...
    6. Setting up a database connection
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Setting up a data repository service
      1. How to do it...
      2. How it works...
    8. Scheduling executors
      1. Getting ready
      2. How to do it...
      3. How it works...
  28. Configuring Web Applications
    1. Creating a basic RESTful application
      1. How to do it...
      2. How it works...
    2. Creating Spring Data REST service
      1. How to do it...
      2. How it works...
    3. Configuring custom servlet filters
      1. How to do it...
      2. How it works...
    4. Configuring custom interceptors
      1. How to do it...
      2. How it works...
    5. Configuring custom HttpMessageConverters
      1. How to do it...
      2. How it works...
    6. Configuring custom PropertyEditors
      1. How to do it...
      2. How it works...
    7. Configuring custom type formatters
      1. How to do it...
      2. How it works...
  29. Web Framework Behavior Tuning
    1. Introduction
    2. Configuring route matching patterns
      1. How to do it...
      2. How it works...
    3. Configuring custom static path mappings
      1. How to do it...
      2. How it works...
    4. Tuning Tomcat via ServletWebServerFactory
      1. How to do it...
      2. How it works...
    5. Choosing embedded servlet containers
      1. How to do it...
      2. How it works...
    6. Adding custom connectors
      1. Getting ready
      2. How to do it...
      3. How it works...
  30. Writing Custom Spring Boot Starters
    1. Introduction
    2. Understanding Spring Boot autoconfiguration
      1. How to do it...
      2. How it works...
    3. Creating a custom Spring Boot autoconfiguration starter
      1. How to do it...
      2. How it works...
    4. Configuring custom conditional bean instantiations
      1. How to do it...
      2. How it works...
    5. Using custom @Enable annotations to toggle configuration
      1. How to do it...
      2. How it works...
  31. Application Testing
    1. Introduction
    2. Creating tests for MVC controllers
      1. How to do it...
      2. How it works...
    3. Configuring a database schema and populating it
      1. How to do it...
      2. How it works...
        1. Initializing the database with Spring JPA and Hibernate
        2. Initializing the database with Spring JDBC
    4. Creating tests using an in-memory database
      1. How to do it...
      2. How it works...
    5. Creating tests using mock objects
      1. How to do it...
      2. How it works...
    6. Creating a JPA component test
      1. How to do it...
      2. How it works...
    7. Creating a WebMvc component test
      1. How to do it...
      2. How it works...
    8. Writing tests using Cucumber
      1. How to do it...
      2. How it works...
    9. Writing tests using Spock
      1. How to do it...
      2. How it works...
  32. Application Packaging and Deployment
    1. Introduction
    2. Creating a Spring Boot executable JAR
      1. How to do it...
      2. How it works...
    3. Creating Docker images
      1. How to do it...
      2. How it works...
    4. Building self-executing binaries
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Spring Boot environment configuration, hierarchy, and precedence
      1. How to do it...
      2. How it works...
    6. Adding a custom PropertySource to the environment using EnvironmentPostProcessor
      1. How to do it...
      2. How it works...
    7. Externalizing an environmental configuration using property files
      1. How to do it...
      2. How it works...
    8. Externalizing an environmental configuration using environment variables
      1. How to do it...
      2. How it works...
    9. Externalizing an environmental configuration using Java system properties
      1. How to do it...
      2. How it works...
    10. Externalizing an environmental config using JSON
      1. How to do it...
      2. How it works...
    11. Setting up Consul
      1. How to do it...
      2. How it works...
    12. Externalizing an environmental config using Consul and envconsul
      1. Getting ready
      2. How to do it...
      3. How it works...
  33. Health Monitoring and Data Visualization
    1. Introduction
    2. Writing custom health indicators
      1. How to do it...
      2. How it works...
    3. Configuring management context
      1. How to do it...
      2. How it works...
    4. Emitting metrics
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Monitoring Spring Boot via JMX
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Managing Spring Boot via SSHd Shell and writing custom remote Shell commands
      1. How to do it...
      2. How it works...
    7. Integrating Micrometer metrics with Graphite
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Integrating Micrometer metrics with Dashing
      1. Getting ready
      2. How to do it...
      3. How it works...
  34. Spring Boot DevTools
    1. Introduction
    2. Adding Spring Boot DevTools to a project
      1. How to do it...
      2. How it works...
    3. Configuring LiveReload
      1. How to do it...
      2. How it works...
    4. Configuring dynamic application restart triggers
      1. How to do it...
      2. How it works...
    5. Using Remote Update
      1. How to do it...
      2. How it works...
  35. Spring Cloud
    1. Introduction
    2. Getting started with Spring Cloud
      1. How to do it...
      2. How it works...
    3. Service discovery using Spring Cloud Consul
      1. How to do it...
      2. How it works...
    4. Using Spring Cloud Netflix – Feign
      1. How to do it...
      2. How it works...
    5. Service discovery using Spring Cloud Netflix – Eureka
      1. How to do it...
      2. How it works...
    6. Using Spring Cloud Netflix – Hystrix
      1. How to do it...
      2. How it works...
  36. Bibliography

Product information

  • Title: Developing Java Applications with Spring and Spring Boot
  • Author(s): Claudio Eduardo de Oliveira, Greg L. Turnquist, Alex Antonov
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789534757