博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kotlin整合Vertx开发Web应用
阅读量:6870 次
发布时间:2019-06-26

本文共 7859 字,大约阅读时间需要 26 分钟。

今天我们尝试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-integration
jar
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
Description
test
description
-Xdoclint:none

添加Vertx实例


  • 创建CoreVerticle类文件
package com.edurt.kvi.coreimport io.vertx.core.AbstractVerticleimport io.vertx.core.Futureimport io.vertx.core.Handlerimport io.vertx.ext.web.Routerimport io.vertx.ext.web.RoutingContextclass 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) } /** * create router instance */ val handlerRoot = Handler
{ req -> req.response().end("Hello Kotlin Vertx Integration!") }}
  • 设置启动类
package com.edurt.kviimport com.edurt.kvi.core.CoreVerticleimport io.vertx.core.Vertxclass KotlinVertxIntegrationfun main(args: Array
) { val vertx = Vertx.vertx() vertx.deployVerticle(CoreVerticle::class.java.name)}

以上操作在vertx.deployVerticle阶段执行了部署Verticle的操作,即部署CoreVerticle。

  • 启动应用后浏览器访问出现以下页面

blob.jpg

增加页面渲染功能


  • 修改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.routerimport io.vertx.ext.web.Routerimport io.vertx.ext.web.RoutingContextimport io.vertx.ext.web.templ.ThymeleafTemplateEngineimport org.thymeleaf.templatemode.TemplateModeclass HomeViewRouterfun 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.coreimport com.edurt.kvi.router.indeximport io.vertx.core.AbstractVerticleimport io.vertx.core.Futureimport io.vertx.core.Handlerimport io.vertx.core.Vertximport io.vertx.core.http.HttpServerResponseimport io.vertx.ext.web.Routerimport io.vertx.ext.web.RoutingContextclass 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)    }    /**     * create router instance     */    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") }}
  • 启动应用后浏览器访问出现以下页面

blob.jpg

转载地址:http://flpfl.baihongyu.com/

你可能感兴趣的文章
SVN服务器搭建和使用(三)
查看>>
JSON入门指南--客户端处理JSON
查看>>
Objective-C内存布局
查看>>
qsort的另类玩法,无聊写着耍耍
查看>>
每日一乐,健康多滋味~~
查看>>
[Oracle] - Connect to a PDB of Oracle12c
查看>>
VS2015 android 设计器不能可视化问题解决。
查看>>
移动数据统计平台分析
查看>>
httppp 1.4.0 发布,HTTP响应时间监控
查看>>
ASP.NET MVC加载ASCX之后,并为之赋值
查看>>
使用SDWebImage淡入淡出的方式加载图片
查看>>
nmon与nmonanalyser系统性能分析
查看>>
52. N-Queens II
查看>>
字符串匹配算法
查看>>
LAMP学习路线图
查看>>
MySQL入门(四)
查看>>
详解 ML2 Core Plugin(II) - 每天5分钟玩转 OpenStack(72)
查看>>
httpd-2.2 配置及用法完全攻略
查看>>
IntelliJ_编译一直报错“找不到符号”
查看>>
【Mongodb】3.X 配置身份验证
查看>>