溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-20 19:33:11 來源:腳本之家 閱讀:123 作者:qianmoQ 欄目:移動(dòng)開發(fā)

今天我們嘗試Kotlin整合Vertx,并決定建立一個(gè)非常簡(jiǎn)單的Web應(yīng)用程序,使用Kotlin和Vertx作為編程語(yǔ)言進(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)以下頁(yè)面

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

增加頁(yè)面渲染功能

修改pom.xml文件增加頁(yè)面依賴

<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)用后瀏覽器訪問http://localhost:8080/index.html出現(xiàn)以下頁(yè)面

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI