Building Reactive Backend App with Spring Boot WebFlux

BRAJENDRA PANDEY
3 min readAug 1, 2021

--

Spring framework 5.0 added a new feature called WebFlux for an asynchronous, non-blocking, event-driven approach.

Basic Concepts of Reactive Approach

  • Publisher: Emits a sequence of events to subscribers according to the demand received from its subscribers.
  • Subscriber: Receives and processes events emitted by a Publisher.
  • Subscription: Defines a one-to-one relationship between a Publisher and a Subscriber.
  • Processors: processing data between publishers and subscribers.
  • Mono: implements Publisher and returns 0 or 1 element.
  • Flux: implement the Publisher and return n elements.

Getting Started 🎬

In this article we will use a sample reactive application using Spring Boot Webflux with MongoDB database, we are using Spring Security for Authorization/Authentication, Lombok for reducing Boilerplate code, and MapStruct for Java bean mappings.

If you would like to refer to the full code, do check:

https://github.com/Brajendra/springboot-reactive-starter-kit

Dependencies ⚙️

<!--Webflux for Reactive-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!--MongoDB->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<!--Spring Security for Authentication/Authorization->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!--POJO mapper-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
<scope>provided</scope>
</dependency>
<!--JWT for Accesss token-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>
<!-- Swagger Open API -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webflux-ui</artifactId>
<version>1.5.9</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.5.9</version>
</dependency>

Package Structure

Config: Initializes app Specific configurations.
Controller: Containes all exposed api.
Entity: Containes DTO, enums, dao and object mappers.
Errors: Handles app specific errors and exceptions.
Repository: Performs all database related activities.
Security: Handles Authorization and Authentication.
Service: Contains Business Logic.

Models:

User entity is used to store user information and also manage granted authorities with the role.

API Controllers

  • AuthController: Exposing Auth-specific API and allow users to signup and login into the app.

Repositories

  • UserRepository: Used to store user details reactively.
@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String> {

Mono<UserDetails> findByEmail(final String email);

Mono<User> findUserByEmail(final String email);
}

Services

  • AuthService: Contains business logic to allow users to signup and login.
public interface AuthService {

Mono<UserDto> signup(UserDto userDto);

Mono<ResponseDto> login(String email, String password);
}

Spring Security

We have implemented JWT Token bases Authentication and Authorization.

  • AuthenticationManager: Manage user role and validate token.
  • SecurityContextRepository: Extracting Bearer token from the header and share this token to AuthenticationManager for Validation of role and token.
  • SecurityConfig: Security Configuration is the main entry point of the application to manage API security and check which API we have to allow and which API we have to restrict.

Testing Application

We can test our application through postman or swagger open API. The following swagger UI result is displayed based on our application.

http://localhost:9090/webjars/swagger-ui/index.html?url=http://localhost:9090/v3/api-docs

--

--