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

这里我就简单的聊几句,如何用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>

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/95613.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

调试器通用波形显示工具

前言&#xff1a;事情起因是我们实验室买了个无线调试器是CMSIS-DAP的&#xff0c;无法使用J-SCOPE显示波形来方便调PID&#xff0c;所以我就在网上找到了个开源工具链接&#xff1a;http://t.csdnimg.cn/ZqZPY使用方法&#xff1a;工具是好工具&#xff0c;就是没有使用手册&a…

操作系统知识

操作系统基础 什么是操作系统&#xff1f; 通过以下四点可以概括操作系统到底是什么&#xff1a; 操作系统&#xff08;Operating System&#xff0c;简称 OS&#xff09;是管理计算机硬件与软件资源的程序&#xff0c;是计算机的基石。操作系统本质上是一个运行在计算机上的…

golang gin框架1——简单案例以及api版本控制

gin框架 gin是golang的一个后台WEB框架 简单案例 package mainimport ("github.com/gin-gonic/gin""net/http" )func main() {r : gin.Default()r.GET("/ping", func(c *gin.Context) {//以json形式输出&#xff0c;还可以xml protobufc.JSON…

二、互联网技术——网络协议

文章目录 一、OSI与TCP/IP参考模型二、TCP/IP参考模型各层功能三、TCP/IP参考模型与对应协议四、常用协议与功能五、常用协议端口 一、OSI与TCP/IP参考模型 二、TCP/IP参考模型各层功能 三、TCP/IP参考模型与对应协议 例题&#xff1a;TCP/IP模型包含四个层次&#xff0c;由上至…

您的报告生成器可以动态执行此操作吗?ViewPro可以

ViewPro for .NET 和 ActiveX&#xff1a;报告生成器、打印引擎和打印预览 ViewPro 允许您将打印预览和报告生成器集成到您的 .NET 和 VB6 项目以及其他项目中。您可以使用 ViewPro 构建基于图形和文本的报告或技术绘图&#xff0c;在表单上的滚动和缩放查看器中显示结果&…

Ubuntu无法引导启动的修复

TLDR&#xff1a;使用Boot-Repair工具。 Boot-Repair Boot-Repair是一个简单的工具&#xff0c;用于修复您在Ubuntu中可能遇到的常见启动问题&#xff0c;例如在安装Windows或其他Linux发行版后无法启动Ubuntu时&#xff0c;或者在安装Ubuntu后无法启动Windows时&#xff0c;…

【C语言】善于利用指针(一)

&#x1f497;个人主页&#x1f497; ⭐个人专栏——C语言初步学习⭐ &#x1f4ab;点击关注&#x1f929;一起学习C语言&#x1f4af;&#x1f4ab; 目录 导读&#xff1a; 1. 什么是指针 1.1 概念 1.2 图解 1.3 示例 2. 指针和指针类型 2.1 指针的定义 2.2 指针的解引…

【图像处理GIU】图像分割(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

想要精通算法和SQL的成长之路 - 并查集的运用和案例(省份数量)

想要精通算法和SQL的成长之路 - 并查集的运用 前言一. 并查集的使用和模板1.1 初始化1.2 find 查找函数1.3 union 合并集合1.4 connected 判断相连性1.5 完整代码 二. 运用案例 - 省份数量 前言 想要精通算法和SQL的成长之路 - 系列导航 一. 并查集的使用和模板 先说一下并查集…

工厂管理软件中的PLM管理

第一部分&#xff1a;介绍PLM管理的定义和背景 1.1 定义&#xff1a;PLM管理是指通过工厂管理软件实现对产品生命周期各个阶段的全面管理和协同合作&#xff0c;包括产品设计、工艺规划、生产制造、质量控制、供应链管理等环节。 1.2 背景&#xff1a;随着市场竞争的加剧和消…

【ORM】浅聊C#和Java的ORM底层框架

给自己一个目标&#xff0c;然后坚持一段时间&#xff0c;总会有收获和感悟&#xff01; 国庆假期马上结束&#xff0c;闲暇时间&#xff0c;突然对Ado.Net这个词的由来感兴趣&#xff0c;然后就一顿复习了一遍&#xff0c;顺便也了解了下java关于ORM框架的底层是什么&#xff…

Gmail 将停止支持基本 HTML 视图

根据 Google 支持文档的更新内容&#xff0c;Gmail 将从明年 1 月起停止支持基本 HTML 视图。 ▲ Gmai 基本 HTML 视图界面 目前网页版 Gmail 提供两个界面&#xff1a;基本 HTML 视图和标准视图。停止支持基本 HTML 视图后&#xff0c;当前打开经典模式的基本 HTML 视图模式 …

一文解释mapState的来龙去脉

mapState Vuex 提供的辅助函数之一&#xff0c;将 store 中的状态映射到组件的计算属性中&#xff0c;使得在组件中可以轻松地访问 Vuex store 中的状态值 MapState(映射状态) 在我们的 Count.vue 组件中&#xff0c;可以使用 mapState 来更简洁地获取 count 的状态值 首先&…

python读取vivo手机截图,将满屏图片文件移动别的路径

问题之初 python读取vivo手机截图&#xff0c; 将满屏图片文件移动别的路径好多这样的图片&#xff0c;占用手机大量的内存&#xff0c;食之无味弃之可惜&#xff01;那么会复制粘贴&#x1f440;代码的我们我们今天就把这些图片筛选清理掉。 这段代码 原有逻辑的基础上&…

基于知识蒸馏的两阶段去雨去雪去雾模型学习记录(二)之知识收集阶段

前面学习了模型的构建与训练过程&#xff0c;然而在实验过程中&#xff0c;博主依旧对数据集与模型之间的关系有些疑惑&#xff0c;首先是论文说这是一个混合数据集&#xff0c;但事实上博主在实验时是将三个数据集分开的&#xff0c;那么在数据读取时是如何混合的呢&#xff0…

林沛满-Wireshark的提示

本文整理自&#xff1a;《Wireshark网络分析的艺术 第1版》 作者&#xff1a;林沛满 著 出版时间&#xff1a;2016-02 最近有不少同事开始学习 Wireshark&#xff0c;他们遇到的第一个困难就是理解不了主界面上的提示信息&#xff0c;于是跑来问我。问的人多了&#xff0c;我也…

如何一步步优化负载均衡策略

发展到一定阶段后&#xff0c;Web 应用程序就会增长到单服务器部署无法承受的地步。这时候企业要么提升可用性&#xff0c;要么提升可扩展性&#xff0c;甚至两者兼而有之。为此&#xff0c;他们会将应用程序部署在多台服务器上&#xff0c;并在服务器之前使用负载均衡器来分配…

华为云云耀云服务器L实例评测|SpringCloud相关组件——nacos和sentinel的安装和配置 运行内存情况 服务器被非法登陆尝试的解决

前言 最近华为云云耀云服务器L实例上新&#xff0c;也搞了一台来玩&#xff0c;期间遇到各种问题&#xff0c;在解决问题的过程中学到不少和运维相关的知识。 本篇博客介绍SpringCloud相关组件——nacos和sentinel的安装和配置&#xff0c;并分析了运行内存情况&#xff0c;此…

数据在内存中的存储(1)

文章目录 目录1. 数据类型介绍1.1 类型的基本归类 2. 整形在内存中的存储2.1 原码、反码、补码2.2 大小端介绍2.3 练习 附&#xff1a; 目录 数据类型介绍整形在内存中的存储大小端字节序介绍及判断浮点型在内存中的存储 1. 数据类型介绍 前面我们已经学习了基本的内置类型以…

Ubuntu20配置Mysql常用操作

文章目录 版权声明ubuntu更换软件源Ubuntu设置静态ipUbuntu防火墙ubuntu安装ssh服务Ubuntu安装vmtoolsUbuntu安装mysql5.7Ubuntu安装mysql8.0Ubuntu卸载mysql 版权声明 本博客的内容基于我个人学习黑马程序员课程的学习笔记整理而成。我特此声明&#xff0c;所有版权属于黑马程…