今天我们尝试Kotlin整合Vertx,并决定建立一个非常简单的Web应用程序,使用Kotlin和Vertx作为编程语言进行编码构建。
生成项目
打开控制台窗口执行以下代码进行生成一个maven项目
复制代码 代码如下:mvn archetype:generate -DgroupId=com.edurt.kvi -DartifactId=kotlin-vertx-integration -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
修改pom.xml增加java和kotlin的支持
4.0.0 com.edurt.kvi kotlin-vertx-integrationjar 1.0.0 kotlin-vertx-integration Kotlin Vertx Integration is a open source kotlin vertx integration example. 1.2.71 3.4.1 3.3 2.10.4 1.2.71 1.8 UTF-8 UTF-8 1.8 1.8 org.jetbrains.kotlin kotlin-stdlib-jdk8${dependency.kotlin.version} org.jetbrains.kotlin kotlin-reflect${dependency.kotlin.version} io.vertx vertx-core${dependency.vertx.ersion} io.vertx vertx-web${dependency.vertx.ersion} 3.5.0 ${project.basedir}/src/main/kotlin ${project.basedir}/src/test/kotlin kotlin-maven-plugin org.jetbrains.kotlin -Xjsr305=strict spring jpa all-open org.jetbrains.kotlin kotlin-maven-allopen${plugin.maven.kotlin.version} org.jetbrains.kotlin kotlin-maven-noarg${plugin.maven.kotlin.version} kapt kapt src/main/kotlin org.springframework.boot spring-boot-configuration-processor${project.parent.version} org.apache.maven.plugins maven-compiler-plugin${plugin.maven.compiler.version} ${environment.compile.java.version} ${environment.compile.java.version} org.apache.maven.plugins maven-javadoc-plugin${plugin.maven.javadoc.version} true -Xdoclint:none Description test description
添加Vertx实例
创建CoreVerticle类文件
package com.edurt.kvi.core
import io.vertx.core.AbstractVerticle
import io.vertx.core.Future
import io.vertx.core.Handler
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
class CoreVerticle : AbstractVerticle() {
override fun start(startFuture: Future?) {
val router = createRouter()
val port = config().getInteger("http.port", 8080)
vertx.createHttpServer()
.requestHandler { router.accept(it) }
.listen(port) { result ->
if (result.succeeded()) {
startFuture?.complete()
} else {
startFuture?.fail(result.cause())
}
}
}
private fun createRouter() = Router.router(vertx).apply {
get("/").handler(handlerRoot)
}
val handlerRoot = Handler { req ->
req.response().end("Hello Kotlin Vertx Integration!")
}
}
设置启动类
package com.edurt.kvi import com.edurt.kvi.core.CoreVerticle import io.vertx.core.Vertx class KotlinVertxIntegration fun main(args: Array) { val vertx = Vertx.vertx() vertx.deployVerticle(CoreVerticle::class.java.name) }
以上操作在vertx.deployVerticle阶段执行了部署Verticle的操作,即部署CoreVerticle。
启动应用后浏览器访问http://localhost:8080出现以下页面
增加页面渲染功能
修改pom.xml文件增加页面依赖
1.7.25 io.vertx vertx-web-templ-thymeleaf${dependency.vertx.ersion} org.slf4j slf4j-log4j12${dependency.slf4j.version}
增加页面渲染文件
package com.edurt.kvi.router
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
import io.vertx.ext.web.templ.ThymeleafTemplateEngine
import org.thymeleaf.templatemode.TemplateMode
class HomeViewRouter
fun index(r: Router) {
val engine = ThymeleafTemplateEngine.create().setMode(TemplateMode.HTML)
r.get("/index.html").handler { c ->
render(c, engine, "templates/index.html")
}
}
fun render(c: RoutingContext, engine: ThymeleafTemplateEngine, templ: String) {
engine.render(c, templ) { res ->
if (res.succeeded()) {
c.response().end(res.result())
} else {
c.fail(res.cause())
}
}
}
在templates/index.html目录下创建页面文件
Kotlin Vertx Integration Welcome To Kotlin Vertx Integration!
修改CoreVerticle增加页面跳转
package com.edurt.kvi.core
import com.edurt.kvi.router.index
import io.vertx.core.AbstractVerticle
import io.vertx.core.Future
import io.vertx.core.Handler
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServerResponse
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
class CoreVerticle : AbstractVerticle() {
override fun start() {
val router = createRouter(vertx)
// go to index page
index(router)
vertx.createHttpServer().requestHandler { handler -> router.accept(handler) }.listen(8080)
// val port = config().getInteger("http.port", 8080)
// vertx.createHttpServer()
// .requestHandler { router.accept(it) }
// .listen(port) { result ->
// if (result.succeeded()) {
// startFuture?.complete()
// } else {
// startFuture?.fail(result.cause())
// }
// }
}
private fun createRouter() = Router.router(vertx).apply {
get("/").handler(handlerRoot)
}
val handlerRoot = Handler { req ->
req.response().end("Hello Kotlin Vertx Integration!")
}
fun createRouter(v: Vertx): Router {
var router = Router.router(v)
router.route("/").handler { c -> c.response().end("Hello Kotlin Vertx Integration!") }
router.route("/index").handler { c -> c.response().html().end("Hello Kotlin Vertx Integration Page!") }
return router
}
fun HttpServerResponse.html(): HttpServerResponse {
return this.putHeader("content-type", "text/html")
}
}
启动应用后浏览器访问http://localhost:8080/index.html出现以下页面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



