On this page
Kotlin
This project provides various extensions and improvements to the Micronaut and Kotlin experience.
For this project, you can find a list of releases (with release notes) here:
The micronaut-kotlin-runtime dependency adds the following features:
-
Support for defining configuration with config4k.
-
A runtime dependency on
jackson-module-kotlin
To enable the above features add the dependency to your build:
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")Config4k Support
Config4k is a type safe configuration format for Kotlin based on HOCON (Human-Optimized Config Object Notation). Configuration files are defined using the conf extension. The following an example configuration file:
src/main/resources/application.confmicronaut {
server {
port = 8081
}
}
test-property = "bad-value"
custom {
user = ${USER}
}Ktor is a Kotlin framework for building connected applications. The micronaut-ktor module includes support for using Ktor as the server instead of Micronaut’s native HTTP server.
This allows users familiar with Ktor to use Micronaut features such as Dependency Injection, AOP, configuration management and so on.
implementation("io.micronaut.kotlin:micronaut-ktor")|
Tip
|
See the Example application which demonstrates how to setup a Micronaut Ktor application |
The entry point for a Micronaut Ktor application is an Application class. An example class can be seen below:
Application classTo define Ktor modules you can create classes that subclass io.micronaut.ktor.KtorApplicationBuilder to install plugins or io.micronaut.ktor.KtorRoutingBuilder to configure routes.
For example, the following installs the Jackson plugin when ktor-serialization-jackson is on the classpath:
To build application routes you can use the KtorRoutingBuilder:
The micronaut-kotlin-extension-functions dependency adds a variety of convenience functions to make using
micronaut with kotlin more user-friendly. For example, reified type parameters
help alleviate the need for using ::class.java in many places where it would otherwise be required.
implementation("io.micronaut.kotlin:micronaut-kotlin-extension-functions")|
Tip
|
See the guide for Using Kotlin Extension Functions in the Micronaut Framework to learn more. |
|
Note
|
You will need to import the functions like this: import io.micronaut.kotlin.FUNCTION_NAME
|
|
Warning
|
Do be aware when defining extension functions in this project or your own, an extension that shadows a member function may have unexpected behavior, and does not throw a compiler error but rather warns. See this Kotlin discussion topic for the latest information. |
BlockingHttpClient Extensions
Single Response Extensions
// Compare exchange usage
val exchangeOneConventional: HttpResponse<Hero> = client.exchange(HttpRequest.GET<Any>("/heroes/any"), Argument.of(Hero::class.java))
val exchangeOneReified: HttpResponse<Hero> = client.exchangeObject<Hero>(HttpRequest.GET("/heroes/any"))
// Compare retrieve usage
val retrieveOneConventional: Hero = client.retrieve(HttpRequest.GET<Any>("/heroes/any"), Argument.of(Hero::class.java))
val retrieveOneReified: Hero = client.retrieveObject<Hero>(HttpRequest.GET("/heroes/any"))List Response Extensions
// Compare exchange usage
val exchangeListConventional: HttpResponse<MutableList<Hero>> = client.exchange(HttpRequest.GET<Any>("/heroes/list"), Argument.listOf(Hero::class.java))
val exchangeListReified: HttpResponse<List<Hero>> = client.exchangeList<Hero>(HttpRequest.GET("/heroes/list"))
// Compare retrieve usage
val retrieveListConventional: MutableList<Hero> = client.retrieve(HttpRequest.GET<Any>("/heroes/list"), Argument.listOf(Hero::class.java))
val retrieveListReified: List<Hero> = client.retrieveList<Hero>(HttpRequest.GET("/heroes/list"))HttpMessage Extensions
// Compare body member usage (Not exceptional path)
val exchangeOneConventionalBody: Hero? = exchangeOneConventional.body.getOrNull()
val exchangeOneReifiedBody: Hero? = exchangeOneReified.bodyOrNull
// Compare getBody() usage (Exceptional path)
val exchangeOneConventionalCustomException: HttpClientResponseException = assertThrows<HttpClientResponseException> {
client.exchange(HttpRequest.GET<Any>("/heroes/conflict"), Argument.of(Hero::class.java))
}
val exchangeOneReifiedCustomException: HttpClientResponseException = assertThrows<HttpClientResponseException> {
client.exchangeObject<Hero>(HttpRequest.GET("/heroes/conflict"))
}
val exchangeOneConventionalCustomExceptionBody: HeroJsonError? = exchangeOneConventionalCustomException.response.getBody(HeroJsonError::class.java).getOrNull()
val exchangeOneReifiedCustomExceptionBody: HeroJsonError? = exchangeOneReifiedCustomException.response.getBodyObject<HeroJsonError>()object Application
fun main(args: Array<String>) {
startApplication<Application>(*args) {
packages("org.example.app")
mapError<RuntimeException> { 500 }
}
}This section documents breaking changes for Kotlin and Ktor versions:
Micronaut Kotlin 5.1.0
Micronaut Kotlin’s Ktor module is now based on Ktor 3.x and includes breaking changes relative to the previous Ktor 2-based integration.
Key changes include Ktor 3 environment and server bootstrap redesign, including the move from ApplicationEngineEnvironmentBuilder to ApplicationEnvironmentBuilder, the introduction of ServerConfig, and the change in embeddedServer() to return EmbeddedServer.
For more information, refer to the following Ktor guide:
You can find the source code of this project in this repository: