Skip to content

feat: add Micronaut Kotlin example #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions with-micronaut-kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Gradle
.gradle/
build/

# IntelliJ IDEA
.idea/
*.iml
*.iws
*.ipr
out/

# VS Code
.vscode/

# Environment variables
.env

# Logs
*.log

# Compiled class files
*.class

# Package files
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# Micronaut
.micronaut/
54 changes: 54 additions & 0 deletions with-micronaut-kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Micronaut Kotlin with Neon Postgres

This example demonstrates how to connect a Micronaut Kotlin application to a Neon Postgres database.

## Prerequisites

- [JDK 11+](https://www.oracle.com/java/technologies/javase-downloads.html)
- [Gradle](https://gradle.org/install/) (or use the included Gradle wrapper)
- [Neon account](https://neon.tech)

## Setup

1. Create a Neon project from the [Neon Console](https://console.neon.tech)
2. Get your connection string from the Neon Console
3. Create a `.env` file in the project root with your Neon credentials:

```
JDBC_DATABASE_URL=jdbc:postgresql://<user>:<password>@<endpoint_hostname>.neon.tech:<port>/<dbname>?sslmode=require
JDBC_DATABASE_USERNAME=<user>
JDBC_DATABASE_PASSWORD=<password>
```

## Running the application

```bash
# Using the Gradle wrapper
./gradlew run
```

The application will start on port 8080. You can test it with:

```bash
# Get all books
curl http://localhost:8080/books

# Get a specific book
curl http://localhost:8080/books/1

# Create a new book
curl -X POST -H "Content-Type: application/json" -d '{"title":"The Great Gatsby","author":"F. Scott Fitzgerald"}' http://localhost:8080/books
```

## Project Structure

- `src/main/kotlin/com/example/entity/Book.kt` - Entity class representing a book
- `src/main/kotlin/com/example/repository/BookRepository.kt` - Repository interface for database operations
- `src/main/kotlin/com/example/controller/BookController.kt` - REST controller for the API
- `src/main/resources/application.yml` - Application configuration including database connection
- `src/main/resources/db/migration/V1__create_book_table.sql` - Database migration script

## Learn More

- [Neon Documentation](https://neon.tech/docs)
- [Micronaut Documentation](https://micronaut.io/documentation/)
73 changes: 73 additions & 0 deletions with-micronaut-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
plugins {
id("org.jetbrains.kotlin.jvm") version "1.9.22"
id("org.jetbrains.kotlin.plugin.allopen") version "1.9.22"
id("org.jetbrains.kotlin.kapt") version "1.9.22"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("io.micronaut.application") version "4.3.2"
}

version = "0.1"
group = "com.example"

repositories {
mavenCentral()
}

dependencies {
// Kotlin
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

// Micronaut
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")

// Database
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("io.micronaut.data:micronaut-data-jdbc")
implementation("org.postgresql:postgresql")
implementation("io.micronaut.flyway:micronaut-flyway")

// Logging
runtimeOnly("ch.qos.logback:logback-classic")

// Testing
testImplementation("io.micronaut.test:micronaut-test-junit5")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-engine")
}

application {
mainClass.set("com.example.ApplicationKt")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "17"
javaParameters = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "17"
javaParameters = true
}
}
}

micronaut {
version("4.2.0")
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.example.*")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading