Getting started with Spring WebFlux and R2DBC in Spring Boot
Tags: tutorial, java, kotlin, springboot
Published: 2019-11-01 17:04:35



# Introduction

In this short article I will try to introduce you to [R2DBC](https://r2dbc.io/) using [Spring Data R2DBC](https://github.com/spring-projects/spring-data-r2dbc) and in the world of reactive applications with [Spring WebFlux](https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux).

What is R2DBC and why should you bother looking at it? First, R2DBC stands for Reactive Relational Database Connectivity. What does that mean? In simple words, R2DBC brings the possibility of non-blocking calls to our Relational Databases (SQL)!

Quoting https://r2dbc.io/, R2DBC is in a nutshell:

* **Reactive Streams - R2DBC** is founded on Reactive Streams providing a fully reactive non-blocking API.
* **Relational Databases - R2DBC** engages SQL databases with a reactive API, something not possible with the blocking nature of JDBC.
* **Scalable Solutions - Reactive Streams** makes it possible to move from the classic one thread per connection approach to a more powerful, more scalable approach.
* **Open Specification - R2DBC** is an open specification establishing a SPI that driver vendors can implement and clients can consume.

In this tutorial, we will build a simple REST API with [Kotlin](https://kotlinlang.org/), [Spring WebFlux]((https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux)) and we will connect to our [H2 Database](https://www.h2database.com/html/main.html) using [Spring Data R2DBC](https://spring.io/projects/spring-data-r2dbc).

# Getting Started

If you want to easily get started with a Springboot project I recommend always using the [Spring Initializr](https://start.spring.io). 
> The Second Best Place on the Internet @ [Josh Long] (https://github.com/joshlong).

## Gradle Configuration

First of all, we have to configure our `build.gradle.kts` and include the dependencies:


{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=build.gradle %}

For the complete `build.gradle.kts` check here:
[build.gradle.kts](https://github.com/Petros0/springboot-full-reactive-kotlin/blob/master/build.gradle.kts).

## Application Configuration

Spring Boot comes with a handful of auto-configurations for Spring Data R2DBC out-of-the-box.
Below we are leveraging the power of Spring Boot to create a connection to the database:

{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=application.yml %}

Then we create a table called `Employee` and we populate it with some data:

{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=ApplicationConfiguration.kts %}

While that may not seem really safe, in our application we do not have any problems, while the database will always be created on startup and destroyed on shutdown of the application.

## REST API

Ok, so we created our database and we populated it with data, now what? Let's make use of it and create a REST API!

Below we have a simple domain model called `Employee`.

{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=Employee.kts %}

Then, let's create our repository, which will provide some queries out-of-the-box and we will also define one of our own. 

{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=EmployeeRepository.kts %}

Last but not least, let's create our endpoint:

{% gist https://gist.github.com/Petros0/98572b6af90edd69de4928f7b713bd8b file=EmployeeController.kts %}

## Result

Let's execute our application with the command `./gradlew bootRun`.

The application now runs on http://localhost:8080!! Congratulations!

# Closing thoughts

Spring Data R2DBC is still on experimental stage. Although it is on the official release of Spring Boot 2.2.0, I would not recommend using it on production :).

I hope you now have a better understanding of R2DBC.

You can find the repository right here:
{% github https://github.com/Petros0/spring-boot-r2dbc-kotlin %}

# Resources

* [R2DBC](https://r2dbc.io/)
* [Spring Data R2DBC](https://spring.io/projects/spring-data-r2dbc)
* [Spring WebFlux](https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux)