这里我就简单的聊几句,如何用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的更多相关文章

  1. 学习vi和vim编辑(3):一个简单的文本编辑器(2)

    然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...

  2. [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993 shiro官网:http://shiro.apache.org/ shi ...

  3. opengl学习笔记(五):组合变换,绘制一个简单的太阳系

    创建太阳系模型 描述的程序绘制一个简单的太阳系,其中有一颗行星和一颗太阳,用同一个函数绘制.需要使用glRotate*()函数让这颗行星绕太阳旋转,并且绕自身的轴旋转.还需要使用glTranslate ...

  4. pipelinewise 学习二 创建一个简单的pipeline

    pipelinewise 提供了方便的创建简单pipeline的命令,可以简化pipeline 的创建,同时也可以帮我们学习 生成demo pipeline pipelinewise init --n ...

  5. Openfire/XMPP学习之——一个简单的Smack样例

    昨天讲了Openfire的搭建和配置,今天来讲一下Smack.如果对如何搭建和配置Openfire的,可以参考Openfire/XMPP学习之——Openfire的安装.配置. Smack是一个开源, ...

  6. Django 学习笔记之六 建立一个简单的博客应用程序

    最近在学习django时建立了一个简单的博客应用程序,现在把简单的步骤说一下.本人的用的版本是python 2.7.3和django 1.10.3,Windows10系统 1.首先通过命令建立项目和a ...

  7. WCF学习——构建一个简单的WCF应用(一)

    本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些准备开始学习WCF的初学者提供一些帮助. 在这个例子中,我们将实现一个简单的计算器和传统的分布式通信框架一样,WC ...

  8. 一起学习造轮子(二):从零开始写一个Redux

    本文是一起学习造轮子系列的第二篇,本篇我们将从零开始写一个小巧完整的Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Red ...

  9. 一起学习造轮子(一):从零开始写一个符合Promises/A+规范的promise

    本文是一起学习造轮子系列的第一篇,本篇我们将从零开始写一个符合Promises/A+规范的promise,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Pr ...

  10. 一起学习造轮子(三):从零开始写一个React-Redux

    本文是一起学习造轮子系列的第三篇,本篇我们将从零开始写一个React-Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Re ...

随机推荐

  1. Amiya 前端UI

    最近在使用一个基于Ant Design 二次封装的组件 Git文档地址 Index - Amiya (gitee.io)

  2. python机器学习经典算法代码示例及思维导图(数学建模必备)

    最近几天学习了机器学习经典算法,通过此次学习入门了机器学习,并将经典算法的代码实现并记录下来,方便后续查找与使用. 这次记录主要分为两部分:第一部分是机器学习思维导图,以框架的形式描述机器学习开发流程 ...

  3. 论文解读(CTDA)《Contrastive transformer based domain adaptation for multi-source cross-domain sentiment classification》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Contrastive transformer based domain adaptation for m ...

  4. 2、搭建MyBatis

    2.1.开发环境 IDE:idea 2019.2 构建工具:maven 3.8.4 MySQL版本:MySQL 5.7 MyBatis版本:MyBatis 3.5.7 MySQL不同版本的注意事项 ( ...

  5. java实现的类似于sql join操作的工具类,通用递归,最低需要java8

    直接上代码,缺包的自行替换为自己项目中存在的 import java.util.ArrayList; import java.util.Collection; import java.util.Has ...

  6. LeetCode46全排列(回溯入门)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 题目描述 难度:中等 给定一个不含重复数字的数组 nu ...

  7. 2023-09-01:用go语言编写。给出两个长度均为n的数组, A = { a1, a2, ... ,an }, B = { b1, b2, ... ,bn }。 你需要求出其有多少个区间[L,R]

    2023-09-01:用go语言编写.给出两个长度均为n的数组, A = { a1, a2, ... ,an }, B = { b1, b2, ... ,bn }. 你需要求出其有多少个区间[L,R] ...

  8. ORM查询一个表中有两个字段相同时,只获取某个值最大的一条

    Table表如下: 获取表中name和hex值相同时age最大的那一条 ORM写法,两次查询 ids = table.values('name', 'age').annotate(id=Max('id ...

  9. Mac m2使用实现微信小程序抓包

    Mac m2使用实现微信小程序抓包 最近换了MacBook Pro,芯片是M2 Pro,很多东西跟windows是不一样的,所以重新配置相应环境,这里介绍一下微信小程序抓包的方法. 使用burp+pr ...

  10. EXE一机一码加密大师1.3.0更新

    EXE一机一码打包加密大师可以打包加密保护EXE文件,同时给EXE文件添加上一机一码认证,或者静态密码,不同的电脑打开加密后的文件需要输入不同的激活码才能正常使用,保护文件安全,方便向用户收费. 1. ...