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

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

hot3.png

今天我们尝试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

转载于:https://my.oschina.net/shicheng2014/blog/3015149

你可能感兴趣的文章
JqueryEasyUI教程入门篇
查看>>
从c++看js的那些概念
查看>>
php获取机器网卡的物理(MAC)地址
查看>>
vim调用python格式化json数据
查看>>
Enum遇到下拉框
查看>>
你知道C#中的Lambda表达式的演化过程吗
查看>>
maven jetty debug 无法关联第三方类库解决办法
查看>>
LNMP环境的安装配置
查看>>
Saltstack数据系统Grains和Pillar(三)
查看>>
24种设计模式
查看>>
Linux下搭建SVN服务
查看>>
jprofiler_监控远程linux服务器的JVM进程(实践)
查看>>
linux 学习之路(学linux必看)
查看>>
域scope 介绍,及查找数据
查看>>
go-import下划线的作用
查看>>
Flink – Stream Task执行过程
查看>>
机器学习第1课:引言(Introduction)
查看>>
iOS 输入时键盘处理问题
查看>>
win7旗舰版显示不了文件扩展名
查看>>
springMVC--动态验证码实现
查看>>