compile 'io.micronaut.kotlin:micronaut-kotlin-runtime:1.0.0.M1'
Micronaut Kotlin Integrations
Includes extensions to Micronaut for Kotlin
Version: 1.0.0.M1
1 Introduction
This project provides various extensions and improvements to the Micronaut and Kotlin experience.
2 Kotlin Runtime Support
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:
<dependency>
<groupId>io.micronaut.kotlin</groupId>
<artifactId>micronaut-kotlin-runtime</artifactId>
<version>1.0.0.M1</version>
</dependency>
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.conf
micronaut { server { port = 8081 } }
3 Ktor Support
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.
See the Example application which demonstrates how to setup a Micronaut Ktor application |
3.1 The KtorApplication class
The entry point for a Micronaut Ktor application is an Application
class. An example class can be seen below:
Application
classimport io.ktor.server.netty.NettyApplicationEngine
import io.micronaut.ktor.*
import org.slf4j.LoggerFactory
import javax.inject.Singleton
@Singleton
class Application : KtorApplication<NettyApplicationEngine.Configuration>({ (1)
applicationEngineEnvironment { (2)
log = LoggerFactory.getLogger(Application::class.java)
}
applicationEngine { (3)
workerGroupSize = 10
}
})
fun main(args: Array<String>) { (4)
runApplication(args)
}
1 | The Application class extends io.micronaut.ktor.KtorApplication and provides the server type (in this case Netty). |
2 | You can optionally configure the ApplicationEngineEnvironment. |
3 | You can optionally configure the ApplicationEngine . In this case NettyApplicationEngine instance’s workerGroupSize is set to 10 |
4 | A main method is defined for running the application within a runnable JAR |
3.2 Defining Ktor Modules
To define Ktor modules you can create classes that subclass io.micronaut.ktor.KtorApplicationBuilder
. Modules can be used to define routes or install Ktor features.
For example the following will install the Jackson feature (when ktor-jackson
is on the classpath):
import com.fasterxml.jackson.databind.SerializationFeature
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.jackson.jackson
import io.micronaut.ktor.KtorApplicationBuilder
import javax.inject.Singleton
@Singleton
class GreetingConfiguration : KtorApplicationBuilder( { (1)
install(ContentNegotiation) { (2)
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
})
1 | The class subclasses KtorApplicationBuilder |
2 | The ContentNegotiation feature is installed and Jackson configured |
To build application routes you can use the routing
module provided by Ktor:
import io.ktor.application.call
import io.ktor.response.respond
import io.ktor.routing.*
import io.micronaut.ktor.KtorApplicationBuilder
import javax.inject.Singleton
@Singleton
class GreetingRoutes(val greetingService: GreetingService) : KtorApplicationBuilder({ (1)
routing { (2)
get("/") {
call.respond(greetingService.greet())
}
get("/demo") {
call.respond(greetingService.greet())
}
}
})
1 | The class again subclasses KtorApplicationBuilder and uses dependency injection to reference GreetingService . |
2 | The routing module is used to build the application routes |