Spring Boot Reactive Tutorial

Mohit Sinha
2 min readAug 26, 2017

--

1. Overview

Spring 5 which will release later this year will support building asynchronous & Reactive applications.

This is a simple tutorial showing the new features in Spring and how to create a web application. The application will connect to a database, have Basic authentication and be Reactive.

2. Reactive Programming

Reactive programming is about building asynchronous, non-blocking and event-driven applications that can easily scale.

Each event is published to subscribers while ensuring that the subscribers are never overwhelmed.

Mono & Flux are implementations of the Publisher interface. A Flux will observe 0 to N items and eventually terminate successfully or not. A Mono will observe 0 or 1 item, with Mono<Void> hinting at most 0 item.

3. Dependencies

We’ll use Gradle to build our project. I recommend using Spring Initializr for bootstrapping your project.

We’ll use:

  • Spring Boot 2
  • Spring Webflux
  • Spring Reactive Data MongoDb
  • Spring Security Reactive Webflux
  • Lombok

All the Spring libraries don’t have a stable release yet.

Lombok is used to reduce boilerplate code for models & POJO’s. It can generate setters/getters, default constructors, toString etc. methods automatically.

Dependencies

4. Auto-configuration

We’ll leave Spring Boot to automatically configure our application based on the dependencies added.

Auto-configuration using annotations

For using non-default values in our application configuration, we can specify them as properties and Spring Boot will automatically use them to create beans.

application properties

All beans necessary for MongoDB, Web and Security will be automatically created.

5. Database

We’ll be using MongoDB in our example and a simple POJO. PersonRepository bean will be created automatically.

Person and the corresponding PersonRepository

6. Web API

We’ll create REST endpoints for Person.

Spring 5 added support for creating routes functionally while still supporting the traditional annotation-based way of creating them.

Let’s look at both of them with the help of examples.

6.1. Annotation-based

This is the traditional way of creating endpoints.

Annotation based endpoint

This will create a REST endpoint /person which will return all the Person records reactively.

6.2. Router Functions

This is a new and concise way of creating endpoints.

Functional way of defining endpoints

The nest method is used to create nested routes, where a group of routes share a common path (prefix), header, or other RequestPredicate.

So, in our case all the corresponding routes have the common prefix /person.

In the first route, we have exposed a GET API /person/{id} which will retrieve the corresponding record and return it.

In the second route, we have exposed a POST API /person which will receive a Person object and save it in the DB.

The cURL commands for the same:

curl http://localhost:8080/person -v -u tom:passwordcurl http://localhost:8080/person/{id} -v -u tom:passwordcurl http://localhost:8080/person -X POST                           -d '{"name":"John Doe","age":20}'                                   -H "Content-Type: application/json" -v -u tom:password

We should define the routes in a Spring configuration file.

7. Security

We’ll be using a very simple Basic authentication mechanism in our example.

We have added some users for our application and assigned different roles to them.

8. Conclusion

I have tried explaining with a simple example, how to build a simple Reactive web application using Spring Boot.

You can read more about:-

You can find the complete example on Github.

--

--

Mohit Sinha

I am a software developer by profession. Other than that I like to read history, philosophy and economics. I have good interest & knowledge on vintage cricket.