• vertx的学习总结7之用kotlin 与vertx搞一个简单的http


    这里我就简单的聊几句,如何用vertx web来搞一个web项目

    1、首先先引入几个依赖,这里我就用maven了,这个是kotlin+vertx web

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>org.example</groupId>
    7. <artifactId>kotlin-vertx</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <properties>
    10. <maven.compiler.source>17</maven.compiler.source>
    11. <maven.compiler.target>17</maven.compiler.target>
    12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    13. <kotlin.version>1.8.20</kotlin.version>
    14. </properties>
    15. <dependencies>
    16. <dependency>
    17. <groupId>io.vertx</groupId>
    18. <artifactId>vertx-core</artifactId>
    19. </dependency>
    20. <dependency>
    21. <groupId>io.vertx</groupId>
    22. <artifactId>vertx-codegen</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>io.vertx</groupId>
    26. <artifactId>vertx-service-proxy</artifactId>
    27. </dependency>
    28. <dependency>
    29. <groupId>io.vertx</groupId>
    30. <artifactId>vertx-web</artifactId>
    31. </dependency>
    32. <dependency>
    33. <groupId>io.vertx</groupId>
    34. <artifactId>vertx-auth-common</artifactId>
    35. </dependency>
    36. <dependency>
    37. <groupId>ch.qos.logback</groupId>
    38. <artifactId>logback-classic</artifactId>
    39. <version>1.2.3</version>
    40. </dependency>
    41. <dependency>
    42. <groupId>org.jetbrains.kotlinx</groupId>
    43. <artifactId>kotlinx-coroutines-core</artifactId>
    44. <version>1.7.1</version>
    45. </dependency>
    46. <dependency>
    47. <groupId>org.jetbrains.kotlin</groupId>
    48. <artifactId>kotlin-stdlib-jdk8</artifactId>
    49. <version>${kotlin.version}</version>
    50. </dependency>
    51. <dependency>
    52. <groupId>org.jetbrains.kotlin</groupId>
    53. <artifactId>kotlin-test</artifactId>
    54. <version>${kotlin.version}</version>
    55. <scope>test</scope>
    56. </dependency>
    57. </dependencies>
    58. <build>
    59. <plugins>
    60. <plugin>
    61. <groupId>org.jetbrains.kotlin</groupId>
    62. <artifactId>kotlin-maven-plugin</artifactId>
    63. <version>${kotlin.version}</version>
    64. <executions>
    65. <execution>
    66. <id>compile</id>
    67. <phase>compile</phase>
    68. <goals>
    69. <goal>compile</goal>
    70. </goals>
    71. <configuration>
    72. <sourceDirs>
    73. <source>src/main/java</source>
    74. <source>target/generated-sources/annotations</source>
    75. </sourceDirs>
    76. </configuration>
    77. </execution>
    78. <execution>
    79. <id>test-compile</id>
    80. <phase>test-compile</phase>
    81. <goals>
    82. <goal>test-compile</goal>
    83. </goals>
    84. </execution>
    85. </executions>
    86. <configuration>
    87. <jvmTarget>${maven.compiler.target}</jvmTarget>
    88. </configuration>
    89. </plugin>
    90. <plugin>
    91. <groupId>org.apache.maven.plugins</groupId>
    92. <artifactId>maven-compiler-plugin</artifactId>
    93. <executions>
    94. <execution>
    95. <id>default-compile</id>
    96. <phase>none</phase>
    97. </execution>
    98. <execution>
    99. <id>default-testCompile</id>
    100. <phase>none</phase>
    101. </execution>
    102. <execution>
    103. <id>compile</id>
    104. <phase>compile</phase>
    105. <goals>
    106. <goal>compile</goal>
    107. </goals>
    108. </execution>
    109. <execution>
    110. <id>testCompile</id>
    111. <phase>test-compile</phase>
    112. <goals>
    113. <goal>testCompile</goal>
    114. </goals>
    115. </execution>
    116. </executions>
    117. </plugin>
    118. </plugins>
    119. </build>
    120. <dependencyManagement>
    121. <dependencies>
    122. <dependency>
    123. <groupId>io.vertx</groupId>
    124. <artifactId>vertx-dependencies</artifactId>
    125. <version>4.4.4</version>
    126. <type>pom</type>
    127. <scope>import</scope>
    128. </dependency>
    129. </dependencies>
    130. </dependencyManagement>
    131. </project>

    2、先创建一个简单的httpweb

    1. package org.example.kotlin_web
    2. import io.vertx.core.AbstractVerticle
    3. import io.vertx.core.Vertx
    4. import io.vertx.core.http.HttpServerOptions
    5. import io.vertx.ext.web.Router
    6. import kotlinx.coroutines.delay
    7. class HttpWeb : AbstractVerticle() {
    8. override fun start() {
    9. var router = Router.router(vertx);
    10. router.get("/hello").handler { context ->
    11. context.response().end("Hello World")
    12. };
    13. vertx.createHttpServer().requestHandler(router).listen(8080)
    14. }
    15. }
    16. fun main(){
    17. var vertx = Vertx.vertx();
    18. vertx.deployVerticle(HttpWeb())
    19. }

    这里用了路由,也就是说访问localhost:8080/hello  它会输出Hello World,这个是get请求

    3、get请求带参数

    1. package org.example.kotlin_web
    2. import io.vertx.core.AbstractVerticle
    3. import io.vertx.core.Vertx
    4. import io.vertx.ext.web.Router
    5. class HttpWeb : AbstractVerticle() {
    6. override fun start() {
    7. var router = Router.router(vertx);
    8. router.get("/hello").handler { ctx ->
    9. val name: String = ctx.request().getParam("name")
    10. // 处理逻辑
    11. val message = "Hello, $name!"
    12. // 返回响应
    13. ctx.response().end(message)
    14. };
    15. vertx.createHttpServer().requestHandler(router).listen(8080)
    16. }
    17. }
    18. fun main(){
    19. var vertx = Vertx.vertx();
    20. vertx.deployVerticle(HttpWeb())
    21. }

    可以看到非常简单

    4、post请求带参数

    1. package org.example.kotlin_web
    2. import io.vertx.core.AbstractVerticle
    3. import io.vertx.core.Vertx
    4. import io.vertx.core.buffer.Buffer
    5. import io.vertx.ext.web.Router
    6. import io.vertx.ext.web.RoutingContext
    7. import io.vertx.ext.web.handler.StaticHandler
    8. class HttpWeb : AbstractVerticle() {
    9. override fun start() {
    10. var router = Router.router(vertx);
    11. router.route().handler(StaticHandler.create("src/main/resources/static").setCachingEnabled(false).setDefaultContentEncoding("UTF-8"));
    12. router.get("/hello").handler { ctx ->
    13. val name: String = ctx.request().getParam("name")
    14. // 处理逻辑
    15. val message = "Hello, $name!"
    16. // 返回响应
    17. ctx.response().end(message)
    18. };
    19. router.post().path("/index").handler { ctx: RoutingContext ->
    20. val request = ctx.request()
    21. val response = ctx.response()
    22. response.putHeader("Content-Type", "text/plain; charset=utf-8")
    23. val formAttributes = request.formAttributes()
    24. request.bodyHandler { body: Buffer ->
    25. val formData = body.toString()
    26. println("Received form data: $formData")
    27. response.setStatusCode(200)
    28. response.end("Form submitted successfully")
    29. }
    30. }
    31. vertx.createHttpServer().requestHandler(router).listen(8080)
    32. }
    33. }
    34. fun main(){
    35. var vertx = Vertx.vertx();
    36. vertx.deployVerticle(HttpWeb())
    37. }

    index.html

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>index</title>
    6. </head>
    7. <body>
    8. <form method="post" action="http://localhost:8080/index">
    9. 姓名: <input type="text" name="name" ><br>
    10. 密码: <input type="password" name="password"> <br>
    11. <input type="submit">
    12. </form>
    13. </body>
    14. </html>

    这里的所有代码都写到了同一个文件里面,这样极其的不美观,可以优化一下

  • 相关阅读:
    jenkins安装
    从零开始打造一款基于SpringBoot+SpringCloud的后台权限管理系统
    数学分析习题课讲义习题-第2章-2.4
    linux服务端c++开发工具介绍(vscode版)
    关于ip地址的网页无法访问navigator的gpu、媒体、蓝牙等设备的解决方法
    IDEA中 lombok不生效解决方法
    python中的单例模型:(详细的单例可以去主页c++中的单例模型)
    C语言 - 文件
    机器学习---CNN(创建和训练一个卷积神经网络并评估其性能)下
    【个人博客搭建】(24)统一api接口返回格式
  • 原文地址:https://blog.csdn.net/m0_63251896/article/details/133577632