vertx的学习总结7

news2024/9/20 16:25:17

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

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

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

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

相关文章

Linux基本指令介绍系列第四篇

文章目录 前言一、Linux基本指令介绍1、more指令2、less指令3、head指令4、tail指令5、bc指令6、管道文件介绍7、与时间相关的指令 总结 前言 本文介绍Linux使用时的部分指令&#xff0c;读者如果想了解更多基本指令的使用&#xff0c;可以关注博主的后续的文章。 博主使用的实…

基于Java的在线听书网站设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作…

阿里云服务器搭建网站(图文新手教程)

使用阿里云服务器快速搭建网站教程&#xff0c;先为云服务器安装宝塔面板&#xff0c;然后在宝塔面板上新建站点&#xff0c;阿里云服务器网以搭建WordPress网站博客为例&#xff0c;来详细说下从阿里云服务器CPU内存配置选择、Web环境、域名解析到网站上线全流程&#xff1a; …

【Zookeeper专题】Zookeeper特性与节点数据类型详解

目录 前言前置知识课程内容一、Zookeeper介绍二、Zookeeper快速开始2.1 Zookeeper安装2.2 客户端命令行操作2.3 GUI工具 三、Zookeeper数据结构3.1 ZNode节点分类3.2 ZNode状态信息3.3 监听机制详解3.3.1 永久性Watch 3.4 节点ZNode特性总结3.5 应用场景详解3.5.1 统一命名服务…

大模型部署手记(7)LLaMA2+Jetson AGX Orin

1.简介 组织机构&#xff1a;Meta&#xff08;Facebook&#xff09; 代码仓&#xff1a;GitHub - facebookresearch/llama: Inference code for LLaMA models 模型&#xff1a;llama-2-7b、llama-2-7b-chat 下载&#xff1a;使用download.sh下载 硬件环境&#xff1a;Jetso…

字符函数和字符串函数(下)

目录 strncpy(Copy characters from string)函数的使用strncat(Append characters from string)函数的使用strncmp(Compare characters of two strings)函数的使用strstr(Locate substring)的使用和模拟实现 感谢各位大佬对我的支持,如果我的文章对你有用,欢迎点击以下链接 &am…

【HUAWEI】VLAN+OSPF+单臂路由

目录 &#x1f96e;写在前面 &#x1f96e;3.1、拓扑图 &#x1f96e;3.2、操作思路 &#x1f96e;3.3、配置操作 &#x1f363;3.3.1、LSW2配置 &#x1f363;3.3.2、LSW3配置 &#x1f363;3.3.3、R1配置 &#x1f363;3.3.4、R2配置 &#x1f363;3.3.5、LSW1配置 &#x1f…

<二>Qt斗地主游戏开发:过场动画的实现

1. 过场动画效果 2. 思路分析 过场动画较为简单&#xff0c;只有一个进度条在进行滚动&#xff0c;因此实现起来不需要动画相关处理&#xff0c;仅需要图片和定时器设定&#xff0c;让进度条动起来即可。我们可以创建一个对话框&#xff0c;设定背景图片以及对话框透明无边框&a…

【数据结构初阶】七、非线性表里的二叉树(堆的实现 -- C语言顺序结构)

相关代码gitee自取&#xff1a; C语言学习日记: 加油努力 (gitee.com) 接上期&#xff1a; 【数据结构初阶】六、线性表中的队列&#xff08;链式结构实现队列&#xff09;-CSDN博客 1 . 非线性表里的 树(Tree) 树的概念及结构&#xff1a; 树的概念 树是一种非线性的数据…

“牛市陷阱?还是回调?是好?还是坏!“

比特币六年来首次在9月实现正回报 比特币回调:发生了什么以及接下来会发生什么? 美元的主导地位&#xff1a;揭示美元涟漪效应 长期持有者持有的比特币供应比例正式达到历史新高 比特币六年来首次在9月实现正回报 随着 10 月份的到来&#xff0c;比特币6年来首次在9月份实…

图像上传功能实现

一、后端 文件存放在images.path路径下 package com.like.common;import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annot…

交通物流模型 | 基于时空注意力融合网络的城市轨道交通假期短时客流预测

短时轨道交通客流预测对于交通运营管理非常重要。新兴的深度学习模型有效提高了预测精度。然而,大部分现有模型主要针对常规工作日或周末客流进行预测。由于假期客流的突发性和无规律性,仅有一小部分研究专注于假期客流预测。为此,本文提出一个全新的时空注意力融合网络(ST…

微信公众号怎么把个人改成企业?

公众号迁移有什么作用&#xff1f;只能变更主体吗&#xff1f;很多小伙伴想做公众号迁移&#xff0c;但是不知道公众号迁移有什么作用&#xff0c;今天跟大家具体讲解一下。首先公众号迁移最主要的就是修改公众号的主体了&#xff0c;比如我们公众号原来是A公司的&#xff0c;现…

Go 语言内置类型全解析:从布尔到字符串的全维度探究

目录 一、布尔类型定义基础用法声明与初始化逻辑运算 进阶用法条件语句循环结构函数返回值 常见错误与陷阱 二、整数类型定义基础用法声明与初始化运算符位运算 进阶用法数据溢出类型转换类型推断 特殊整数类型runebyte 常见问题和陷阱 三、浮点数类型定义基础用法声明与初始化…

IBT机考-PBT笔考,优劣分析,柯桥口语学习,韩语入门,topik考级韩语

IBT机考&#xff0c;顾名思义就是在电脑上答题考试&#xff0c;区别于现在的PBT纸笔答题&#xff0c;不需要发卷、收卷&#xff0c;也不需要填涂和用笔写字。 考试不需要带任何文具&#xff0c;就连笔试要用到的修正带都将省去。因为听力、阅读的选择题都是用鼠标点击&#xf…

深入了解 RabbitMQ:高性能消息中间件

目录 引言&#xff1a;一、RabbitMQ 介绍二、核心概念三、工作原理四、应用场景五、案例实战 引言&#xff1a; 在现代分布式系统中&#xff0c;消息队列成为了实现系统间异步通信、削峰填谷以及解耦组件的重要工具。而RabbitMQ作为一个高效可靠的消息队列解决方案&#xff0c;…

eNSP网络实验

二层VLAN 四台PC的IP地址如图所示&#xff0c;子网掩码均为255.255.255.0&#xff0c;四台PC处在同一个局域网之中&#xff0c;在配置VLAN之前能够彼此ping通。配置的目的是将PC1和PC3划分到VLAN10中&#xff0c;PC2和PC4划分到VLAN20中。 在配置之前需要进入系统视角。 创建V…

点餐小程序实战教程03-用户注册

我们上一篇介绍了如何创建用户数据源&#xff0c;有了数据源之后就需要思考如何判断用户是否注册过。根据用户在系统中的状态来判断是引导到注册页面还是直接显示首页。 1 前端API 判断用户是否注册&#xff0c;需要拿到用户登录状态的信息。我们在上一篇已经分析了微搭支持的…

线程的详解

创建状态 就绪状态 阻塞状态 运行状态 死亡状态 常用方法 setPriority(ing newPriority) 更改线程的优先级 sleep(long millis) 在指定的毫秒数内让当前正在执行的线程休眠 join() 等待该线程终止 yield() 暂停当前正在执行的线程对象&#xff0c;并执行其他线程 inte…

51单片机+EC11编码器实现可调参菜单+OLED屏幕显示

51单片机+EC11编码器实现可调参菜单+OLED屏幕显示 📍相关篇《stc单片机使用外部中断+EC11编码器实现计数功能》 🎈《STC单片机+EC11编码器实现调节PWM输出占空比》 🌼实际操作效果 🍁整个项目实现框架: 📓EC11接线原理图: 📓项目工程简介 📝仅凭借一个EC11编…