### Define Ktor Routes with KtorRoutingBuilder in Kotlin Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This example illustrates defining Ktor application routes using `io.micronaut.ktor.KtorRoutingBuilder`. It shows how to define GET and POST endpoints, handle incoming requests, and use dependency injection for services like `GreetingService`. ```kotlin import io.ktor.server.application.call import io.ktor.server.request.receive import io.ktor.server.response.respond import io.ktor.server.routing.* import io.micronaut.ktor.KtorRoutingBuilder import jakarta.inject.Singleton @Singleton class GreetingRoutes(private val greetingService: GreetingService) : KtorRoutingBuilder({ get("/") { call.respond(greetingService.greet()) } post("/") { val name = call.receive().name call.respond(greetingService.greet(name)) } }) data class CustomGreetingRequest(val name: String) ``` -------------------------------- ### Start Micronaut Application with Kotlin Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This code snippet shows how to start a Micronaut application using Kotlin. It configures the application to scan specific packages and map runtime exceptions to HTTP status codes. ```kotlin object Application fun main(args: Array) { startApplication(*args) { packages("org.example.app") mapError { 500 } } } ``` -------------------------------- ### Example Micronaut Kotlin Application Configuration (application.conf) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Defines type-safe configuration for a Micronaut Kotlin application using Config4k and HOCON format. This example sets the server port and custom properties. ```hocon micronaut { server { port = 8081 } } test-property = "bad-value" custom { user = ${USER} } ``` -------------------------------- ### Define Ktor Features with KtorApplicationBuilder in Kotlin Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This snippet demonstrates how to define Ktor features by subclassing `io.micronaut.ktor.KtorApplicationBuilder`. It shows the installation of the `ContentNegotiation` feature and configuration of Jackson for JSON serialization. ```kotlin import com.fasterxml.jackson.databind.SerializationFeature import io.ktor.server.application.install import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.serialization.jackson.jackson import io.micronaut.ktor.KtorApplicationBuilder import jakarta.inject.Singleton @Singleton class GreetingConfiguration : KtorApplicationBuilder({ install(ContentNegotiation) { jackson { enable(SerializationFeature.INDENT_OUTPUT) } } }) ``` -------------------------------- ### Micronaut Ktor Application Entry Point (Kotlin) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Demonstrates the entry point for a Micronaut Ktor application using the KtorApplication class. It configures the Ktor server, application engine, and includes a main method for running the application. ```kotlin import io.ktor.server.netty.NettyApplicationEngine import io.micronaut.ktor.KtorApplication import io.micronaut.ktor.runApplication import jakarta.inject.Singleton import org.slf4j.LoggerFactory @Singleton class Application : KtorApplication({ applicationEngineEnvironment { log = LoggerFactory.getLogger(Application::class.java) } applicationEngine { workerGroupSize = 10 } }) fun main(args: Array) { runApplication(args) } ``` -------------------------------- ### Demonstrate HttpMessage Extension Usage in Kotlin Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This snippet demonstrates the usage of HttpMessage extensions in Kotlin for accessing request and response bodies, including handling exceptional paths with custom exceptions. It utilizes Micronaut's client and argument mapping. ```kotlin val exchangeOneConventionalBody: Hero? = exchangeOneConventional.body.getOrNull() val exchangeOneReifiedBody: Hero? = exchangeOneReified.bodyOrNull val exchangeOneConventionalCustomException: HttpClientResponseException = assertThrows { client.exchange(HttpRequest.GET("/heroes/conflict"), Argument.of(Hero::class.java)) } val exchangeOneReifiedCustomException: HttpClientResponseException = assertThrows { client.exchangeObject(HttpRequest.GET("/heroes/conflict")) } val exchangeOneConventionalCustomExceptionBody: HeroJsonError? = exchangeOneConventionalCustomException.response.getBody(HeroJsonError::class.java).getOrNull() val exchangeOneReifiedCustomExceptionBody: HeroJsonError? = exchangeOneReifiedCustomException.response.getBodyObject() ``` -------------------------------- ### Demonstrate Reified Type Parameters for BlockingHttpClient in Kotlin Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This code compares conventional and reified type parameter usage with Micronaut's `BlockingHttpClient` for both single and list responses. It highlights how reified types simplify client calls by removing the need for explicit `Argument` definitions. ```kotlin // Compare exchange usage val exchangeOneConventional: HttpResponse = client.exchange(HttpRequest.GET("/heroes/any"), Argument.of(Hero::class.java)) val exchangeOneReified: HttpResponse = client.exchangeObject(HttpRequest.GET("/heroes/any")) // Compare retrieve usage val retrieveOneConventional: Hero = client.retrieve(HttpRequest.GET("/heroes/any"), Argument.of(Hero::class.java)) val retrieveOneReified: Hero = client.retrieveObject(HttpRequest.GET("/heroes/any")) // Compare exchange usage val exchangeListConventional: HttpResponse> = client.exchange(HttpRequest.GET("/heroes/list"), Argument.listOf(Hero::class.java)) val exchangeListReified: HttpResponse> = client.exchangeList(HttpRequest.GET("/heroes/list")) // Compare retrieve usage val retrieveListConventional: MutableList = client.retrieve(HttpRequest.GET("/heroes/list"), Argument.listOf(Hero::class.java)) val retrieveListReified: List = client.retrieveList(HttpRequest.GET("/heroes/list")) ``` -------------------------------- ### Add Micronaut Ktor Dependency (Maven) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Adds the 'micronaut-ktor' dependency to your Maven project, allowing Ktor to be used as the server framework for your Micronaut applications. ```xml io.micronaut.kotlin micronaut-ktor ``` -------------------------------- ### Add Micronaut Kotlin Extension Functions Dependency Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index This snippet shows how to add the `micronaut-kotlin-extension-functions` dependency to your project using Gradle and Maven. This dependency enhances the usability of Micronaut with Kotlin by providing convenient extension functions. ```gradle implementation("io.micronaut.kotlin:micronaut-kotlin-extension-functions") ``` ```maven io.micronaut.kotlin micronaut-kotlin-extension-functions ``` -------------------------------- ### Add Micronaut Ktor Dependency (Gradle) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Integrates the 'micronaut-ktor' dependency into your Gradle build, enabling the use of Ktor as the server within a Micronaut application. ```gradle implementation("io.micronaut.kotlin:micronaut-ktor") ``` -------------------------------- ### Add Micronaut Kotlin Runtime Dependency (Maven) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Includes the 'micronaut-kotlin-runtime' dependency in your Maven project, which provides Config4k support and integrates 'jackson-module-kotlin' at runtime. ```xml io.micronaut.kotlin micronaut-kotlin-runtime ``` -------------------------------- ### Add Micronaut Kotlin Runtime Dependency (Gradle) Source: https://micronaut-projects.github.io/micronaut-kotlin/latest/guide/index Adds the 'micronaut-kotlin-runtime' dependency to your Gradle build, enabling features like Config4k support and a runtime dependency on 'jackson-module-kotlin'. ```gradle implementation("io.micronaut.kotlin:micronaut-kotlin-runtime") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.