An open API service indexing awesome lists of open source software.

https://github.com/devcavin/spring-reactive-kotlin


https://github.com/devcavin/spring-reactive-kotlin

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

          

# Spring Reactive Kotlin Example

A simple **Spring Boot 3** project using **Kotlin**, **WebFlux (coroutines)**, and **H2 (R2DBC)**.
It demonstrates a clean, Kotlin-idiomatic way to build a reactive CRUD REST API.

---

## Features
- Reactive web layer with **Spring WebFlux**
- **Kotlin coroutines** instead of `Flux`/`Mono`
- **H2 (R2DBC)** in-memory database (no setup required)
- Simple **CRUD** endpoints for `User`
- Clean separation of DTOs, entities, repository, service, controller

---

## Project Structure
```
spring-reactive-kotlin/
├── build.gradle.kts # Gradle build (Kotlin DSL)
├── src/main/resources/
│ └── application.properties # Config (H2, R2DBC)
│ └── schema.sql # Creates users table
├── src/main/kotlin/com/example/demo/
│ ├── Application.kt # Main entry
│ ├── User.kt # Entity
│ ├── CreateUserRequest.kt # Request DTO and Response
│ ├── UserRepository.kt
│ ├── UserService.kt
│ └── UserController.kt
```

---

## Requirements
- JDK 17+
- Gradle (wrapper included)

---

## Run the App
```bash
./gradlew bootRun
```

The app will start at **http://localhost:8080**.

---

## API Endpoints

### Create User
```http
POST /users
Content-Type: application/json

{
"name": "Alice",
"email": "alice@example.com"
}
```

### Get All Users
```http
GET /users
```

### Get User by ID
```http
GET /users/{id}
```

### Delete User
```http
DELETE /users/{id}
```

---

## Database
We use **H2 (in-memory)** with **R2DBC**.
Schema is auto-created from [`schema.sql`](src/main/resources/schema.sql):

```sql
CREATE TABLE USERS (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(150)
);
```

The DB is wiped each time the app restarts (great for testing/learning).

---

## Next Steps
- Add **update endpoint**
- Add **tests** with `WebTestClient`
- Explore **error handling** with `@ControllerAdvice`
- Switch to a production DB (e.g., PostgreSQL R2DBC)