Ktor 2.3 发布,Kotlin 编写的异步框架

1,588次阅读
没有评论

Ktor 是一个异步框架,用于创建微服务、Web 应用等。从头到尾都是用 Kotlin 编写的。

Ktor 2.3.0 近日正式发布,这次更新为 Ktor 生态带来了许多新功能和改进。以下部分简要介绍了该版本的主要变化:

核心改进

WebSockets ContentNegotiation 插件的显试类型

WebSocket ContentNegotiation 插件现在支持指定显式类型:

client.webSocket("ws://localhost:8080/echo") {
val originalData: Any = Data("hello")
sendSerialized(originalData, typeInfo<Data>())
val received = receiveDeserialized<Data>(typeInfo<Data>())

assertEquals(originalData, received)
}

Socket的结构化并发支持

来自 ktor-network 的 Socket 实现现在是一个 CoroutineScope。这意味着它支持 cancel() 操作来立即终止连接。

close()继续作为正常关闭工作,并将等待所有待定操作完成。

抛弃 JS 遗留配置

即将发布的 Kotlin 1.9.0 版本将删除遗留的 JS 编译器。从 Ktor 2.3.0 开始,传统的编译器不再被支持。请考虑迁移到新的 IR 编译器。

服务器

路由中的正则表达式支持

Ktor 现在允许在定义路由时使用正则表达式。使用正则表达式路由,你可以匹配一个复杂的路由,并轻松地捕获路由参数。


routing {
get(Regex("^(?<name>.+)$")) {
val name = call.parameters["name"]
call.respondText("Hello, $name!")
}
}

Static Content API 清理

Static Content API 已经被完善和精简,以获得更友好的用户体验。这次我们把重点放在最常见的情况上,并为它们做了一个简单的 API:

routing {
staticFiles("/static", File("files"))
}

将为 /static 路径下的 files目录中的所有文件提供服务。旧的 API 仍然可用,并将被支持。

而且我们也可以从资源中提供文件:

支持 CIO 的 100 Continue

CIO 引擎现在支持 100 Continue 状态。它开箱即用,不需要额外配置。

支持多个配置文件

通过允许使用多个文件,配置管理得到了简化。你可以使用 -config CLI 选项传递多个配置文件。配置文件将被依次加载和合并。

支持 Jetty 11 和 Tomcat 10

对于已经包含 Jetty 或 Tomcat 依赖关系的项目,Ktor 2.3.0 增加了对 Jetty 11 和 Tomcat 10 服务器实现的支持。它们是作为单独的依赖项交付的:

dependencies {
implementation("io.ktor:ktor-server-jetty-jakarta:2.3.0")
implementation("io.ktor:ktor-server-tomcat-jakarta:2.3.0")
implementation("io.ktor:ktor-server-servlet-jakarta:2.3.0")
}

更多详情可查看:https://blog.jetbrains.com/ktor/2023/04/18/ktor-2-3-0-released/

Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 4 月
 12
3456789
10111213141516
17181920212223
24252627282930
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别 量子位的朋友们 2025-1...