今天我们尝试kotlin整合vertx,并决定建立一个非常简单的web应用程序,使用kotlin和vertx作为编程语言进行编码构建。
生成项目
打开控制台窗口执行以下代码进行生成一个maven项目
修改pom.xml增加java和kotlin的支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<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类文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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!" ) } } |
设置启动类
1
2
3
4
5
6
7
8
9
10
11
|
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
阶段执行了部署verticle的操作,即部署coreverticle。
启动应用后浏览器访问http://localhost:8080出现以下页面
增加页面渲染功能
修改pom.xml文件增加页面依赖
1
2
3
4
5
6
7
8
9
10
11
12
|
<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> |
增加页面渲染文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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目录下创建页面文件
1
2
3
4
5
6
7
8
9
10
11
|
<!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增加页面跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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" ) } } |
启动应用后浏览器访问http://localhost:8080/index.html出现以下页面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000018311021