资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Kotlin整合Vertx开发Web应用

今天我们尝试Kotlin整合Vertx,并决定建立一个非常简单的Web应用程序,使用Kotlin和Vertx作为编程语言进行编码构建。

创新互联建站长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为灵武企业提供专业的网站建设、成都网站制作,灵武网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

生成项目

打开控制台窗口执行以下代码进行生成一个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.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)
 }

 /**
  * create router instance
  */
 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出现以下页面

Kotlin整合Vertx开发Web应用

增加页面渲染功能

修改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)
 }

 /**
  * 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")
 }

}

启动应用后浏览器访问http://localhost:8080/index.html出现以下页面

Kotlin整合Vertx开发Web应用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前文章:Kotlin整合Vertx开发Web应用
URL网址:http://cdkjz.cn/article/jpcdjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220