vertx的学习总结7

2023-10-05 16:39
文章标签 学习 总结 vertx

本文主要是介绍vertx的学习总结7,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>kotlin-vertx</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><kotlin.version>1.8.20</kotlin.version></properties><dependencies><dependency><groupId>io.vertx</groupId><artifactId>vertx-core</artifactId></dependency><dependency><groupId>io.vertx</groupId><artifactId>vertx-codegen</artifactId></dependency><dependency><groupId>io.vertx</groupId><artifactId>vertx-service-proxy</artifactId></dependency><dependency><groupId>io.vertx</groupId><artifactId>vertx-web</artifactId></dependency><dependency><groupId>io.vertx</groupId><artifactId>vertx-auth-common</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.jetbrains.kotlinx</groupId><artifactId>kotlinx-coroutines-core</artifactId><version>1.7.1</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib-jdk8</artifactId><version>${kotlin.version}</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test</artifactId><version>${kotlin.version}</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version><executions><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals><configuration><sourceDirs><source>src/main/java</source><source>target/generated-sources/annotations</source></sourceDirs></configuration></execution><execution><id>test-compile</id><phase>test-compile</phase><goals><goal>test-compile</goal></goals></execution></executions><configuration><jvmTarget>${maven.compiler.target}</jvmTarget></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><executions><execution><id>default-compile</id><phase>none</phase></execution><execution><id>default-testCompile</id><phase>none</phase></execution><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals></execution></executions></plugin></plugins></build><dependencyManagement><dependencies><dependency><groupId>io.vertx</groupId><artifactId>vertx-dependencies</artifactId><version>4.4.4</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

2、先创建一个简单的httpweb

package org.example.kotlin_webimport io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServerOptions
import io.vertx.ext.web.Router
import kotlinx.coroutines.delayclass HttpWeb : AbstractVerticle() {override fun start() {var router = Router.router(vertx);router.get("/hello").handler { context ->context.response().end("Hello World")};vertx.createHttpServer().requestHandler(router).listen(8080)}
}
fun main(){var vertx = Vertx.vertx();vertx.deployVerticle(HttpWeb())
}

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

3、get请求带参数

package org.example.kotlin_webimport io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.ext.web.Routerclass HttpWeb : AbstractVerticle() {override fun start() {var router = Router.router(vertx);router.get("/hello").handler { ctx ->val name: String = ctx.request().getParam("name")// 处理逻辑val message = "Hello, $name!"// 返回响应ctx.response().end(message)};vertx.createHttpServer().requestHandler(router).listen(8080)}
}
fun main(){var vertx = Vertx.vertx();vertx.deployVerticle(HttpWeb())
}

可以看到非常简单

4、post请求带参数

package org.example.kotlin_webimport io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
import io.vertx.ext.web.handler.StaticHandlerclass HttpWeb : AbstractVerticle() {override fun start() {var router = Router.router(vertx);router.route().handler(StaticHandler.create("src/main/resources/static").setCachingEnabled(false).setDefaultContentEncoding("UTF-8"));router.get("/hello").handler { ctx ->val name: String = ctx.request().getParam("name")// 处理逻辑val message = "Hello, $name!"// 返回响应ctx.response().end(message)};router.post().path("/index").handler { ctx: RoutingContext ->val request = ctx.request()val response = ctx.response()response.putHeader("Content-Type", "text/plain; charset=utf-8")val formAttributes = request.formAttributes()request.bodyHandler { body: Buffer ->val formData = body.toString()println("Received form data: $formData")response.setStatusCode(200)response.end("Form submitted successfully")}}vertx.createHttpServer().requestHandler(router).listen(8080)}
}
fun main(){var vertx = Vertx.vertx();vertx.deployVerticle(HttpWeb())
}

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<form method="post" action="http://localhost:8080/index">姓名:  <input type="text" name="name" ><br>密码:  <input type="password" name="password"> <br><input type="submit">
</form>
</body>
</html>

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

这篇关于vertx的学习总结7的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/151598

相关文章

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

MySQL基本查询示例总结

《MySQL基本查询示例总结》:本文主要介绍MySQL基本查询示例总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Create插入替换Retrieve(读取)select(确定列)where条件(确定行)null查询order by语句li

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Linux区分SSD和机械硬盘的方法总结

《Linux区分SSD和机械硬盘的方法总结》在Linux系统管理中,了解存储设备的类型和特性是至关重要的,不同的存储介质(如固态硬盘SSD和机械硬盘HDD)在性能、可靠性和适用场景上有着显著差异,本文... 目录一、lsblk 命令简介基本用法二、识别磁盘类型的关键参数:ROTA查询 ROTA 参数ROTA

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义