Ktor is an asynchronous web framework written in and designed for Kotlin. Allowing the more impressive features of Kotlin, such as coroutines, to not only be used but supported. Typically, Spring is my go-to general framework, and usually what I use when I need to put a REST API together. However, after recently attending a London Kotlin meetup where there was a presentation on Ktor, I decided I’d try something new for once. That is how I ended up here, writing a blog post about Ktor. So, this post is a learning experience for both you and me.
Implementation
Dependencies
buildscript {
ext.kotlin_version = '1.3.41'
ext.ktor_version = '1.2.2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
// might not be needed but my build kept defaulting to Java 12
java {
disableAutoTargetJvm()
}
// Ktor uses coroutines
kotlin {
experimental {
coroutines "enable"
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
dependencies {
// Kotlin stdlib + test dependencies
// ktor dependencies
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "io.ktor:ktor-jackson:$ktor_version"
// logback for logging
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
// kodein for dependency injection
compile group: 'org.kodein.di', name: 'kodein-di-generic-jvm', version: '6.3.0'
}