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_web
import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServerOptions
import io.vertx.ext.web.Router
import kotlinx.coroutines.delay
class 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_web
import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.ext.web.Router
class 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_web
import 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.StaticHandler
class 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之用kotlin 与vertx搞一个简单的http的更多相关文章
- 学习vi和vim编辑(3):一个简单的文本编辑器(2)
然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993 shiro官网:http://shiro.apache.org/ shi ...
- opengl学习笔记(五):组合变换,绘制一个简单的太阳系
创建太阳系模型 描述的程序绘制一个简单的太阳系,其中有一颗行星和一颗太阳,用同一个函数绘制.需要使用glRotate*()函数让这颗行星绕太阳旋转,并且绕自身的轴旋转.还需要使用glTranslate ...
- pipelinewise 学习二 创建一个简单的pipeline
pipelinewise 提供了方便的创建简单pipeline的命令,可以简化pipeline 的创建,同时也可以帮我们学习 生成demo pipeline pipelinewise init --n ...
- Openfire/XMPP学习之——一个简单的Smack样例
昨天讲了Openfire的搭建和配置,今天来讲一下Smack.如果对如何搭建和配置Openfire的,可以参考Openfire/XMPP学习之——Openfire的安装.配置. Smack是一个开源, ...
- Django 学习笔记之六 建立一个简单的博客应用程序
最近在学习django时建立了一个简单的博客应用程序,现在把简单的步骤说一下.本人的用的版本是python 2.7.3和django 1.10.3,Windows10系统 1.首先通过命令建立项目和a ...
- WCF学习——构建一个简单的WCF应用(一)
本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些准备开始学习WCF的初学者提供一些帮助. 在这个例子中,我们将实现一个简单的计算器和传统的分布式通信框架一样,WC ...
- 一起学习造轮子(二):从零开始写一个Redux
本文是一起学习造轮子系列的第二篇,本篇我们将从零开始写一个小巧完整的Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Red ...
- 一起学习造轮子(一):从零开始写一个符合Promises/A+规范的promise
本文是一起学习造轮子系列的第一篇,本篇我们将从零开始写一个符合Promises/A+规范的promise,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Pr ...
- 一起学习造轮子(三):从零开始写一个React-Redux
本文是一起学习造轮子系列的第三篇,本篇我们将从零开始写一个React-Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Re ...
随机推荐
- FastJson不成想还有个版本2啊:序列化大字符串报错
背景 发现陷入了一个怪圈,写文章的话,感觉只有大bug或比较值得写的内容才会写,每次一写就是几千字,争取写得透彻一些,但这样,我也挺费时间,读者也未必有这么多时间看. 我想着,日常遇到的小bug.平时 ...
- Mybatis-plus SQL效率插件PerformanceInterceptor无效->替换为p6spy
使用mybatis-plus时,需要加入执行的sql分析 发现mybatis-plus中的PerformanceInterceptor无效了 查了信息发现 3.2.0 版本之后把这个功能可剔除了 可同 ...
- 干货分享:用ChatGPT调教批量出Midjourney咒语,出图效率Nice ,附资料。
Prompts就是AI绘图的核心竞争力. 您是不是觉得用Midjourney生成的图不够完美? 又让ChatGPT去生成Prompt,然后效果还不理想? 其实ChatGPT你给他投喂资料后,经过调教的 ...
- Java 配置 HTTP/Socks 代理竟能如此简单
在网络请求过程中,使用代理是一种常见的需求.代理服务器可以帮助我们隐藏真实的 IP 地址.加速访问速度.访问公司特定内网等等要求.在 Java 中,我们可以通过一些库或框架来实现代理的设置和使用. 但 ...
- Llama2-Chinese项目:1-项目介绍和模型推理
Atom-7B与Llama2间的关系:Atom-7B是基于Llama2进行中文预训练的开源大模型.为什么叫原子呢?因为原子生万物,Llama中文社区希望原子大模型未来可以成为构建AI世界的基础单位.目 ...
- Go协程揭秘:轻量、并发与性能的完美结合
Go协程为并发编程提供了强大的工具,结合轻量级.高效的特点,为开发者带来了独特的编程体验.本文深入探讨了Go协程的基本原理.同步机制.高级用法及其性能与最佳实践,旨在为读者提供全面.深入的理解和应用指 ...
- Solution Set -「ABC 192」
「ABC 113A」Star Link. 略. #include<cstdio> int x; int main() { scanf("%d",&x); for ...
- 如何选择适合你的HTAP数据库?
最近,在数据库行业对HTAP(混合事务/分析处理,Hybrid Transactional/Analytical Processing)这个概念宣传的非常火爆,也衍生出 Real-Time HTAP的 ...
- 2020 ICPC 南京站
gym A. Ah, It's Yesterday Once More 有趣的题,但场上的人恐怕不会这么想( 构造一条长路径,且拐弯处在不同边界.这样每条竖线合并后都在一边,还需要走一遍才能合并到一起 ...
- 使用yum管理RPM软件包
yum概念 对比rpm命令,rpm命令需要手动寻找安装该软件包所需要的一系列依赖关系.当软件包需要卸载时,容易由于卸载掉了某个依赖关系而导致其他的软件包不能用. yum(Yellow dog upda ...