Building Reactive REST APIs with Spring boot 3.2.0

Part One — Base set up.

Timz Owen
4 min readDec 3, 2023

In our simple story today, we get to learn how to build CRUD APIs with spring boot. In a growing world where microservices are taking over, applications need to be non-blocking asynchronous and event-driven in handling requests from a server and client, reactive programming plays a major role.

In simple terms, we are allowing callback functions to notify the handlers when the request is ready as we perform other background tasks.

Stack & Version: Java 17+ & Spring boot 3.2.0 + & Maven

Part One: Step by step set up.

  1. set up your dependencies on Spring Initializr
  2. Import & set up your database configurations.
  3. Define your model class.

Part 2: building on the Logic.

  1. Create the Repository Interface
  2. Create Service class.
  3. Create service implementation class.
  4. Create Controller logic and endpoints.
  5. Test your APIs with postman or Swagger-UI

1.0 Setting up requirements.

make sure you have java 17 installed and Postgres for out database connection:

PostgreSQL Driver: R2DBC driver and JDBC allows us to connect to PostgreSQL databases. R2DBC allows reactive data persistence.

Lombok: Use annotation to reduce boilerplate code.

Reactive Web: build reactive apps with help of webFlux.

Now generate and let's import it in our IntelliJ and start configuring the database.

2.0 Database Configuration

Once the project is imported, navigate to application.properties under the resources folder. Rename to application.yml .This is an optional way of formatting your database configuration code.

spring:
r2dbc:
url: r2dbc:postgresql://localhost:5432/reactive_student_db
username: postgres # your database username
password: root # your db password

3.0 Defining your model class.

Let's work with a students’ scenario where we need to manage students for a web client / site. Lets model a student with fields such as: first name — string, age — Integer & last name — string. Rem to also set Id field to identify the relationship in the database.

Annotations needed are Getters & setter, AllArgsConstructor, NoArgsConstructor & Builder. Entity annotation isn't available in reactive and thus Table annotation is used. create package/ Directory and name it as model/entity, then create a student class. N/B: classes and interfaces naming starts with caps case.

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "students")
public class Student {

private Integer id;
private String firstName;
private String lastName;
private int age;
}

To view the tables created, we need to add a dependency for migrations in pom.xml on our project. Let's use flyway dependency.

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

Now add the configuration to yml file under R2DBC but rem to change to JDBC as flyway uses JDBC for connection to database. username and password will remain the same.

  flyway:
user: postgres
password: root
url: jdbc:postgresql://localhost:5432/reactive_student_db

Under Resources, create a folder to handle the migrations. Let's name it as db, and create another directory inside db and name it migration. Inside the migration folder, create a new file V1__init.sql if you get a notification that ‘SQL dialect isn't configure’, change it to Postgres from the IntelliJ recommendation.

Inside your SQL script, lets write some query to create the student properties defined in the model class. NB: The properties {firstname….} define in the Student class must match the Query you are creating.

CREATE TABLE students(
id SERIAL NOT NULL PRIMARY KEY ,
firstname VARCHAR(150),
lastname VARCHAR(250),
age integer
)

You can now run the application and check that the table has columns and a migration been created.

Coming Next…………. Repository, Service & Controllers. Make sure your set up is running before next chapter.

--

--

Timz Owen

Full stack SRE | DevOps | Automation Engineer. I Love building Tech communities @Africa