Kotlin整合Vertx開(kāi)發(fā)Web應(yīng)用
今天我們嘗試Kotlin整合Vertx,并決定建立一個(gè)非常簡(jiǎn)單的Web應(yīng)用程序,使用Kotlin和Vertx作為編程語(yǔ)言進(jìn)行編碼構(gòu)建。
生成項(xiàng)目
打開(kāi)控制臺(tái)窗口執(zhí)行以下代碼進(jìn)行生成一個(gè)maven項(xiàng)目
修改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類(lèi)文件
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)類(lèi)
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)用后瀏覽器訪問(wèn)http://localhost:8080出現(xiàn)以下頁(yè)面

增加頁(yè)面渲染功能
修改pom.xml文件增加頁(yè)面依賴(lài)
<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>
增加頁(yè)面渲染文件
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)建頁(yè)面文件
<!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增加頁(yè)面跳轉(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)用后瀏覽器訪問(wèn)http://localhost:8080/index.html出現(xiàn)以下頁(yè)面

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)之ToggleButton實(shí)現(xiàn)開(kāi)關(guān)效果示例
這篇文章主要介紹了Android開(kāi)發(fā)之ToggleButton實(shí)現(xiàn)開(kāi)關(guān)效果的方法,結(jié)合實(shí)例形式分析了ToggleButton控件實(shí)現(xiàn)開(kāi)關(guān)效果的布局與功能相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android應(yīng)用開(kāi)發(fā)中控制反轉(zhuǎn)IoC設(shè)計(jì)模式使用教程
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中控制反轉(zhuǎn)IoC設(shè)計(jì)模式使用教程,IoC其實(shí)更常被理解為一種依賴(lài)注入的模式,用來(lái)分解業(yè)務(wù)層降低耦合,需要的朋友可以參考下2016-04-04
Android通過(guò)Socket與服務(wù)器之間進(jìn)行通信的示例
這篇文章主要介紹了Android通過(guò)Socket與服務(wù)器之間進(jìn)行通信的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
android socket聊天室功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了android socket聊天室功能實(shí)現(xiàn)方法,不單純是聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 對(duì)話框sweet-alert-dialog
這篇文章主要介紹了Android 對(duì)話框sweet-alert-dialog的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android應(yīng)用開(kāi)發(fā)中CardView的初步使用指南
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中CardView的初步使用指南,CardView主要處理一些卡片型的視圖布局,需要的朋友可以參考下2016-02-02
Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
這篇文章主要給大家介紹了關(guān)于Android如何將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過(guò)示例代碼介紹的非常吸納關(guān)系,對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android?中的監(jiān)聽(tīng)和按鍵處理詳情
這篇文章主要介紹了Android?中的監(jiān)聽(tīng)和按鍵處理詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Android Studio實(shí)現(xiàn)仿微信APP門(mén)戶(hù)界面詳解及源碼
這篇文章帶你通過(guò)Android studio來(lái)實(shí)現(xiàn)微信APP的門(mén)戶(hù)界面,主要說(shuō)明框架的各部分功能與實(shí)現(xiàn)過(guò)程,下文包含了整個(gè)開(kāi)發(fā)過(guò)程,以及解決問(wèn)題的思路并再末尾提供了源碼鏈接2021-10-10
android?studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小功能
這篇文章主要為大家詳細(xì)介紹了android?studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

