Spring 5.0 By Example

Book description

Discover the real power of Spring Framework 5.0 and learn to create powerful applications in its newest version

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
  • Apply your knowledge to build three real-world projects in Spring

Who This Book Is For

If you're a developer starting out with Spring, then this book will help you learn about the new Spring 5.0 framework concepts followed by their implementation in Java and Kotlin. The book will also help experienced Spring developers gain insights into the new features added in Spring 5.0.

What You Will Learn

  • Implement REST APIs with Spring REST support
  • Introduce 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
  • Introduce Reactive Programming and use this with Spring Webflux
  • Implement a Reactive REST client and learn how it can create asynchronous applications
  • Create a robust, scalable, and fault tolerant application with Spring Messaging
  • Implement a websocket to add interactive behaviors in your applications
  • Introduce the Spring Cloud projects

In Detail

With growing demands, organizations are looking for systems that are robust and scalable. Therefore, the 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 book has three parts, where each one covers the building of a comprehensive project in Java and Spring. In the first part, you will construct a CMS Portal using Spring's support for building REST APIs. You will also learn to integrate these APIs with AngularJS and later develop this application in a reactive fashion using Project Reactor, Spring WebFlux, and Spring Data. In the second part, you'll understand how to build a messaging application, which will consume the Twitter API and perform filtering and transformations. Here, you will also learn about server-sent events and explore Spring's support for Kotlin, which makes application development quick and efficient. In the last part, you will build a real microservice application using the most important techniques and patterns such as service discovery, circuit breakers, security, data streams, monitoring, and a lot more from this architectural style.

By the end of the book, you will be confident about using Spring to build your applications.

Style and approach

This book teaches you to use the new version of Spring Framework to solve problems commonly encountered in modern web application; it explains the main features of this version such as support for Reactive programming and Kotlin. You'll learn these features by creating interesting projects and putting them into practice right now.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Spring 5.0 By Example
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. 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
      4. Microservices and 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Spring 5.0 By Example
  • Author(s): Claudio Eduardo de Oliveira
  • Release date: February 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788624398