Configs2026年4月12日·1 分钟阅读

Ktor — Async Web Framework for Kotlin by JetBrains

Ktor is a framework for quickly creating connected applications in Kotlin with minimal effort. Built by JetBrains, it supports coroutines natively, runs on multiple engines (Netty, CIO, Jetty), and works for server + client HTTP in Kotlin Multiplatform.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

# Via IntelliJ IDEA or Gradle
gradle init --type kotlin-application

Add to build.gradle.kts:

plugins {
    id("io.ktor.plugin") version "3.0.0"
    kotlin("plugin.serialization") version "2.0.0"
}

dependencies {
    implementation("io.ktor:ktor-server-core")
    implementation("io.ktor:ktor-server-netty")
    implementation("io.ktor:ktor-server-content-negotiation")
    implementation("io.ktor:ktor-serialization-kotlinx-json")
}
fun main() {
    embeddedServer(Netty, port = 8080) {
        install(ContentNegotiation) { json() }

        routing {
            get("/api/assets") {
                call.respond(listOf(Asset("react", 230000)))
            }
            post("/api/assets") {
                val asset = call.receive<Asset>()
                call.respond(HttpStatusCode.Created, asset)
            }
        }
    }.start(wait = true)
}

@Serializable
data class Asset(val repo: String, val stars: Int)
介绍

Ktor is a framework by JetBrains for quickly creating connected applications in Kotlin. Fully coroutine-based from the ground up, Ktor provides both server and client HTTP functionality. Runs on Netty, CIO, Jetty, or Tomcat engines and supports Kotlin Multiplatform for shared networking code.

What Ktor Does

  • Server — HTTP server with routing, content negotiation
  • Client — HTTP client with multiplatform support
  • Coroutines — native suspend functions throughout
  • Plugins — Authentication, CORS, Sessions, WebSocket, Call Logging
  • Serialization — kotlinx.serialization, Gson, Jackson
  • WebSocket — built-in support
  • Multiple engines — Netty, CIO, Jetty, Tomcat
  • Multiplatform — client works on JVM, iOS, JS, Native

Comparison

Framework Language Async Model
Ktor Kotlin Coroutines
Spring Boot Java/Kotlin Threads + WebFlux
Micronaut Java/Kotlin Compile-time DI
Quarkus Java/Kotlin CDI + Reactive

常见问题 FAQ

Q: Ktor vs Spring Boot? A: Ktor 更轻量、纯 Kotlin、coroutine-native;Spring Boot 生态更大、企业功能更多。小中型 Kotlin 项目用 Ktor 很爽。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产