麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

Kotlin整合Vertx開發Web應用

2024-07-21 23:03:52
字體:
來源:轉載
供稿:網友

今天我們嘗試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的支持

<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實例

創建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<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!") }}

設置啟動類

package com.edurt.kviimport com.edurt.kvi.core.CoreVerticleimport io.vertx.core.Vertxclass KotlinVertxIntegrationfun main(args: Array<String>) { val vertx = Vertx.vertx() vertx.deployVerticle(CoreVerticle::class.java.name)}

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

啟動應用后瀏覽器訪問http://localhost:8080出現以下頁面

Kotlin,Vertx,Web應用

增加頁面渲染功能

修改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.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目錄下創建頁面文件

<!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增加頁面跳轉

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

啟動應用后瀏覽器訪問http://localhost:8080/index.html出現以下頁面

Kotlin,Vertx,Web應用

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到kotlin教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产日韩在线观看视频 | 精品亚洲在线 | 一区二区三区欧美在线 | 蜜桃视频最新网址 | 黄网站在线播放视频免费观看 | va毛片 | 黄色特级大片 | 91精品国产综合久久婷婷香 | 91精品国产综合久久久欧美 | 日日爱99 | 136福利视频 | 日本a级一区 | 女人裸体让男人桶全过程 | 久国久产久精永久网页 | 国产精品久久久久久久亚洲按摩 | 久久久久久久不卡 | a黄网站 | 久久在线精品视频 | 国产影院一区 | 色婷婷久久久亚洲一区二区三区 | 欧美日韩精品不卡一区二区三区 | 久久精品亚洲欧美日韩精品中文字幕 | 欧美成人鲁丝片在线观看 | 亚洲欧美国产视频 | 草逼一区| 久久精品成人免费国产片桃视频 | av黄色在线免费观看 | 91av在线影院| 国内久久久久 | 国产小视频一区 | 免费观看视频在线观看 | www.91sese | 在线成人av观看 | 欧美精品日日鲁夜夜添 | 久久久久久高清 | 亚洲成人精品久久 | 黄视频网站免费 | 日韩av片在线播放 | 黄色7777| 欧美日韩高清在线观看 | 久久久久久久久久久影视 |