Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Example 04. Reactive DB access with R2DBC

ℹ️ About

This example shows the use of R2DBC library for reactive access to a relational database (in our case, Postgres).

It is a small API to perform the typical CRUD operations, in this case with astronauts as a domain model.

With this project we can learn how to configure the library and the basic use of the ReactiveCrudRepository.

📚 Stack

  • Language: Java 15 (SDKMan + AdoptOpenJDK)

  • Framework: Spring Boot 2.4+

  • Libraries: Starter Data R2DBC, R2DBC-postgresql, TestContainers, Postgres driver

  • Test Engine: JUnit 5

  • Test Libraries: AssertJ, RestAssured, JavaFaker

  • Build and dependency management: Gradle

  • Other: Lombok, Actuator

💁‍♀️ Useful information

🚀 How to launch the project?

To use this example you must have a local Postgres database instance. The resources contains two SQL files in order to prepare the database:

  • schema.sql

  • data.sql

Local database is also mandatory to run the tests due to some TestContainers inconsistences.

The example has this initial config:

spring.r2dbc.url=r2dbc:postgresql://localhost/arpj_example_04
spring.r2dbc.username=arpj04
spring.r2dbc.password=arpj04

Be careful and change your database, username and password name in the spring.r2dbc.* properties.

🏹 HTTP Endpoints

The project contains a astronauts-api.http file to run the calls from IntelliJ Idea.

Here you have the cURL versions:

curl -X GET --location "http://localhost:8080/astronauts" -H "Accept: application/json"

curl -X GET --location "http://localhost:8080/astronauts/358" -H "Accept: application/json"

curl -X GET --location "http://localhost:8080/astronauts/random" -H "Accept: application/json"

curl -X GET --location "http://localhost:8080/astronauts/filter?name=Arm" -H "Accept: application/json"

curl -X DELETE --location "http://localhost:8080/astronauts/250" -H "Accept: application/json"

curl -X POST --location "http://localhost:8080/astronauts" -H "Content-Type: application/json" \
    -d "{
          \"name\": \"Susana Pérez\",
          \"status\": \"A\",
          \"birthPlace\": \"Sevilla, ES\",
          \"gender\": \"M\",
          \"spaceFlights\": 15,
          \"spaceWalks\": 100,
          \"missions\": \"All\"
        }"

curl -X PUT --location "http://localhost:8080/astronauts" -H "Content-Type: application/json" \
    -d "{
          \"id\": 358,
          \"name\": \"Santiago Garrido\",
          \"status\": \"A\",
          \"birthPlace\": \"Guadalajara, ES\",
          \"gender\": \"M\",
          \"spaceFlights\": 15,
          \"spaceWalks\": 44,
          \"missions\": \"All\"
        }"

🧪 Integration tests

In addition, the project contains integration tests with RestAssured:

Test output - Astronauts (simplifed)
Started AstronautIntegrationTest in 3.931 seconds (JVM running for 5.549)
### --> Integration Test: Astronauts. Get all astronauts...
### --> Integration Test: Astronauts. Save astronaut...
AstronautHttpResponse(id=365, name=Helena Hanbaskett, status=Active, birthPlace=Port-au-Prince, gender=Female, spaceFlights=26, spaceWalks=93, missions=Losgar)
### --> Integration Test: Astronauts. Get astronaut by id...
AstronautHttpResponse(id=365, name=Helena Hanbaskett, status=Active, birthPlace=Port-au-Prince, gender=Female, spaceFlights=26, spaceWalks=93, missions=Losgar)
### --> Integration Test: Astronauts. Update astronaut...
### --> Integration Test: Astronauts. Get astronaut by id...
AstronautHttpResponse(id=365, name=Val Veeta, status=Deceased, birthPlace=Tallinn, gender=Female, spaceFlights=21, spaceWalks=21, missions=Greenway)
### --> Integration Test: Astronauts. Delete astronaut...