pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Kotlin整合Vertx開發(fā)Web應(yīng)用

這篇文章主要介紹了Kotlin整合Vertx開發(fā)Web應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

今天我們嘗試Kotlin整合Vertx,并決定建立一個(gè)非常簡(jiǎn)單的Web應(yīng)用程序,使用Kotlin和Vertx作為編程語言進(jìn)行編碼構(gòu)建。

生成項(xiàng)目

打開控制臺(tái)窗口執(zhí)行以下代碼進(jìn)行生成一個(gè)maven項(xiàng)目

復(fù)制代碼 代碼如下:
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的支持


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.edurt.kvi</groupId>
 <artifactId>kotlin-vertx-integration</artifactId>
 <packaging>jar</packaging>
 <version>1.0.0</version>

 <name>kotlin-vertx-integration</name>
 <description>Kotlin Vertx Integration is a open source kotlin vertx integration example.</description>

 <!-- properties -->
 <properties>
  <!-- dependency -->
  <dependency.kotlin.version>1.2.71</dependency.kotlin.version>
  <dependency.vertx.ersion>3.4.1</dependency.vertx.ersion>
  <!-- plugin -->
  <plugin.maven.compiler.version>3.3</plugin.maven.compiler.version>
  <plugin.maven.javadoc.version>2.10.4</plugin.maven.javadoc.version>
  <plugin.maven.kotlin.version>1.2.71</plugin.maven.kotlin.version>
  <!-- environment -->
  <environment.compile.java.version>1.8</environment.compile.java.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <jvmTarget>1.8</jvmTarget>
 </properties>

 <!-- dependencys -->
 <dependencies>
  <!-- kotlin -->
  <dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-stdlib-jdk8</artifactId>
   <version>${dependency.kotlin.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-reflect</artifactId>
   <version>${dependency.kotlin.version}</version>
  </dependency>
  <!-- vertx -->
  <dependency>
   <groupId>io.vertx</groupId>
   <artifactId>vertx-core</artifactId>
   <version>${dependency.vertx.ersion}</version>
  </dependency>
  <dependency>
   <groupId>io.vertx</groupId>
   <artifactId>vertx-web</artifactId>
   <version>${dependency.vertx.ersion}</version>
  </dependency>
 </dependencies>

 <!-- prerequisites -->
 <prerequisites>
  <maven>3.5.0</maven>
 </prerequisites>

 <!-- build -->
 <build>
  <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
  <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
  <plugins>
   <plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <configuration>
     <args>
      <arg>-Xjsr305=strict</arg>
     </args>
     <compilerPlugins>
      <plugin>spring</plugin>
      <plugin>jpa</plugin>
      <plugin>all-open</plugin>
     </compilerPlugins>
     <pluginOptions>
      <option>all-open:annotation=javax.persistence.Entity</option>
     </pluginOptions>
    </configuration>
    <dependencies>
     <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-maven-allopen</artifactId>
      <version>${plugin.maven.kotlin.version}</version>
     </dependency>
     <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-maven-noarg</artifactId>
      <version>${plugin.maven.kotlin.version}</version>
     </dependency>
    </dependencies>
    <executions>
     <execution>
      <id>kapt</id>
      <goals>
       <goal>kapt</goal>
      </goals>
      <configuration>
       <sourceDirs>
        <sourceDir>src/main/kotlin</sourceDir>
       </sourceDirs>
       <annotationProcessorPaths>
        <annotationProcessorPath>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-configuration-processor</artifactId>
         <version>${project.parent.version}</version>
        </annotationProcessorPath>
       </annotationProcessorPaths>
      </configuration>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${plugin.maven.compiler.version}</version>
    <configuration>
     <source>${environment.compile.java.version}</source>
     <target>${environment.compile.java.version}</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>${plugin.maven.javadoc.version}</version>
    <configuration>
     <aggregate>true</aggregate>
     <!-- custom tags -->
     <tags>
      <tag>
       <name>Description</name>
       <placement>test</placement>
       <head>description</head>
      </tag>
     </tags>
     <!-- close jdoclint check document -->
     <additionalparam>-Xdoclint:none</additionalparam>
    </configuration>
   </plugin>
  </plugins>
 </build>

</project>

添加Vertx實(shí)例

創(chuàng)建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<Void>?) {
  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<RoutingContext> { req ->
  req.response().end("Hello Kotlin Vertx Integration!")
 }

}

設(shè)置啟動(dòng)類


package com.edurt.kvi

import com.edurt.kvi.core.CoreVerticle
import io.vertx.core.Vertx

class KotlinVertxIntegration

fun main(args: Array<String>) {
 val vertx = Vertx.vertx()
 vertx.deployVerticle(CoreVerticle::class.java.name)
}

以上操作在vertx.deployVerticle階段執(zhí)行了部署Verticle的操作,即部署CoreVerticle。

啟動(dòng)應(yīng)用后瀏覽器訪問http://localhost:8080出現(xiàn)以下頁面

增加頁面渲染功能

修改pom.xml文件增加頁面依賴


<dependency.slf4j.version>1.7.25</dependency.slf4j.version>

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-web-templ-thymeleaf</artifactId>
 <version>${dependency.vertx.ersion}</version>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>${dependency.slf4j.version}</version>
</dependency>

增加頁面渲染文件


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目錄下創(chuàng)建頁面文件


<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Kotlin Vertx Integration</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<body>
<p>Welcome To Kotlin Vertx Integration!</p>
</body>
</html>

修改CoreVerticle增加頁面跳轉(zhuǎn)


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<RoutingContext> { 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")
 }

}

啟動(dòng)應(yīng)用后瀏覽器訪問http://localhost:8080/index.html出現(xiàn)以下頁面

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持html5模板網(wǎng)。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

主站蜘蛛池模板: 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 北京康百特科技有限公司-分子蒸馏-短程分子蒸馏设备-实验室分子蒸馏设备 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 西子馋火锅鸡加盟-太原市龙城酉鼎餐饮管理有限公司 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 | 防潮防水通风密闭门源头实力厂家 - 北京酷思帝克门窗 | 圆周直径尺-小孔内视镜-纤维研磨刷-东莞市高腾达精密工具 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 环球电气之家-中国专业电气电子产品行业服务网站! | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | LINK FASHION 童装·青少年装展 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 定制液氮罐_小型气相液氮罐_自增压液氮罐_班德液氮罐厂家 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 活动策划,舞台搭建,活动策划公司-首选美湖上海活动策划公司 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 淋巴细胞分离液_口腔医疗器材-精欣华医疗器械(无锡)有限公司 | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 |