Detailed description can be found here: Introduction to Reactive APIs with Postgres, R2DBC, Spring Data JDBC and Spring WebFlux
For the latest instruction how to use it with Spring Boot, Spring Data R2DBC, and Spring WebFlux see here: Reactive Spring Boot with WebFlux, R2DBC and Postgres
- Spring Boot: 4.0.4
- Java: 21
- Kotlin: 2.3.10
- Spring WebFlux: Reactive web framework
- Spring Data R2DBC: Reactive database connectivity
- PostgreSQL: Database with R2DBC driver
- Testcontainers: Docker-based integration testing
- Maven: Build tool
This is a multi-module Maven project demonstrating reactive microservices architecture:
sample-spring-data-webflux/
├── employee-service/ # Employee management microservice
└── organization-service/ # Organization management microservice (aggregates employees)
REST API for managing employees with reactive endpoints.
Port: 8090
Endpoints:
GET /employees- Get all employees (returnsFlux<Employee>)GET /employees/{id}- Get employee by ID (returnsMono<Employee>)GET /employees/organization/{organizationId}- Get employees by organizationPOST /employees- Create new employee
Data Model:
class Employee(
val name: String,
val salary: Int,
val organizationId: Int
) {
@Id var id: Int? = null
}Database: PostgreSQL with R2DBC connection
- URL:
r2dbc:postgresql://localhost:5432/spring - Table:
employee(id, name, salary, organization_id)
REST API for managing organizations with inter-service communication using reactive WebClient.
Port: 8095
Endpoints:
GET /organizations- Get all organizations (returnsFlux<Organization>)GET /organizations/{id}- Get organization by ID (returnsMono<Organization>)GET /organizations/{id}/with-employees- Get organization with its employees (aggregated data)POST /organizations- Create new organization
Data Model:
class Organization(var name: String) {
@Id var id: Int? = null
}Database: PostgreSQL with R2DBC connection
- URL:
r2dbc:postgresql://localhost:5432/spring - Table:
organization(id, name)
Inter-Service Communication: Uses reactive WebClient to fetch employees from employee-service
- Java 21+
- Maven 3.6+
- Docker (for Testcontainers in tests)
- PostgreSQL 14+ (or use Testcontainers for local development)
-
Start PostgreSQL database:
docker run -d \ --name postgres \ -e POSTGRES_DB=spring \ -e POSTGRES_USER=spring \ -e POSTGRES_PASSWORD=spring123 \ -p 5432:5432 \ postgres:14
-
Build the project:
mvn clean install
-
Run Employee Service:
cd employee-service mvn spring-boot:run -
Run Organization Service (in another terminal):
cd organization-service mvn spring-boot:run
The project includes Testcontainers support for development mode. You can run the application with an embedded PostgreSQL container:
# Employee Service
cd employee-service
mvn spring-boot:test-run -Dspring-boot.run.mainClass=pl.piomin.service.employee.EmployeeApplicationTestKt
# Organization Service
cd organization-service
mvn spring-boot:test-run -Dspring-boot.run.mainClass=pl.piomin.service.organization.OrganizationApplicationTestKtThe project includes comprehensive integration tests using:
- JUnit 5: Test framework
- Testcontainers: PostgreSQL containers for integration tests
- WebTestClient: Reactive web testing
- MockServer: Mocking HTTP endpoints
# Run all tests
mvn test
# Run tests for specific module
mvn test -pl employee-service
mvn test -pl organization-serviceTest Coverage:
- Employee Service: 4 integration tests
- Organization Service: 6 tests (1 unit test + 5 integration tests)
Both services expose Spring Boot Actuator endpoints:
Actuator Base Path: /actuator
Enabled Endpoints:
/actuator/health- Health check with detailed information/actuator/metrics- Application metrics/actuator/env- Environment properties- And more (all endpoints exposed via
management.endpoints.web.exposure.include: '*')
curl -X POST http://localhost:8095/organizations \
-H "Content-Type: application/json" \
-d '{"name": "Tech Corp"}'curl -X POST http://localhost:8090/employees \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "salary": 75000, "organizationId": 1}'curl http://localhost:8095/organizations/1/with-employeesResponse:
{
"id": 1,
"name": "Tech Corp",
"employees": [
{
"id": 1,
"name": "John Doe",
"salary": 75000,
"organizationId": 1
}
]
}✅ Fully Reactive: Non-blocking I/O throughout the stack using Project Reactor ✅ R2DBC: Reactive database connectivity with PostgreSQL ✅ Kotlin: Concise and expressive code ✅ Spring Boot 4: Latest Spring Boot with enhanced reactive support ✅ Microservices: Two independent services communicating reactively ✅ Testcontainers: Real database integration testing ✅ Service Discovery: WebClient-based inter-service communication ✅ Production Ready: Actuator endpoints for monitoring
This project has been updated to Spring Boot 4.0.4. Key changes include:
- WebTestClient Configuration: No longer auto-configured by default. Manual bean configuration added in test classes.
- WebClient.Builder: Requires explicit bean definition (not auto-configured).
- Test Dependencies: Added
reactor-testandspring-boot-test-autoconfigurefor proper reactive testing support. - JUnit 5: Migrated from JUnit 4 to JUnit 5 (
org.junit.jupiter.api.Test).
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is available for educational and demonstration purposes.