这篇文章将为大家详细讲解有关spring boot中如何实现整合ktor,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
背景
在用了一阵子 Ktor 之后,深感基于协程的方便,但是公司的主要技术栈是 SpringBoot,虽然已经整合了 Kotlin,但是如果有 Ktor 加持则会更加的方便。因此作了一番研究后,也完全可以实现这样的整合了。
建立一个 starter
首先新建一个 Kotlin 项目,在其 build.gradle 内加入对 SpringBoot 和 Ktor 的依赖,并同时加入对打为 jar 包的代码:
dependencies { implementation "org.springframework.boot:spring-boot-starter-aop:${springBootVersion}" implementation "io.ktor:ktor-jackson:${ktorVersion}" compile "io.ktor:ktor-server-netty:${ktorVersion}" compile "io.ktor:ktor-html-builder:${ktorVersion}" testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}" testCompile "io.ktor:ktor-client-apache:${ktorVersion}" testCompile "io.ktor:ktor-server-test-host:${ktorVersion}" } jar { from { configurations.runtime.collect { zipTree(it) } } } task sourceJar(type: Jar) { from sourceSets.main.allSource classifier 'sources' }