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

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

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



    4.0.0

    org.example
    kotlin-vertx
    1.0-SNAPSHOT

    
        17
        17
        UTF-8
        1.8.20
    


    
        
            io.vertx
            vertx-core
        

        
            io.vertx
            vertx-codegen
        

        
            io.vertx
            vertx-service-proxy
        

        
            io.vertx
            vertx-web
        

        
            io.vertx
            vertx-auth-common
        

        
            ch.qos.logback
            logback-classic
            1.2.3
        
        
            org.jetbrains.kotlinx
            kotlinx-coroutines-core
            1.7.1
        
        
            org.jetbrains.kotlin
            kotlin-stdlib-jdk8
            ${kotlin.version}
        
        
            org.jetbrains.kotlin
            kotlin-test
            ${kotlin.version}
            test
        
    

    
        
            
                org.jetbrains.kotlin
                kotlin-maven-plugin
                ${kotlin.version}
                
                    
                        compile
                        compile
                        
                            compile
                        
                        
                            
                                src/main/java
                                target/generated-sources/annotations
                            
                        
                    
                    
                        test-compile
                        test-compile
                        
                            test-compile
                        
                    
                
                
                    ${maven.compiler.target}
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                        default-compile
                        none
                    
                    
                        default-testCompile
                        none
                    
                    
                        compile
                        compile
                        
                            compile
                        
                    
                    
                        testCompile
                        test-compile
                        
                            testCompile
                        
                    
                
            
        
    

    
        
            
                io.vertx
                vertx-dependencies
                4.4.4
                pom
                import
            
        
    

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())
}

可以看到非常简单

vertx的学习总结7之用kotlin 与vertx搞一个简单的http_第1张图片

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())
}

vertx的学习总结7之用kotlin 与vertx搞一个简单的http_第2张图片

index.html




    
    index


姓名:
密码:

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

你可能感兴趣的:(vertx,学习,vertx)