vertx 的http服务表单提交与mysql验证

1、依赖



    4.0.0

    org.example
    kotlin-vertx
    1.0-SNAPSHOT

    
        17
        17
        UTF-8
        1.8.20
    


    
        
            io.vertx
            vertx-core
        

        
            mysql
            mysql-connector-java
            8.0.31
        

        
            io.vertx
            vertx-auth-jdbc
        

        
            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、表单




    
    index


姓名:
密码:

3、完整代码

package org.example.kotlin_web

import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.ext.jdbc.JDBCClient
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext

class PostHttp :AbstractVerticle(){
    override fun start() {
        var router = Router.router(vertx);
        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")
            }
        }
    }
}

fun main(){
    var vertx = Vertx.vertx();
    val jdbcClient = JDBCClient.createShared(
        vertx, JsonObject()
            .put("url", "jdbc:mysql://localhost:3306/vertxTest")
            .put("driver_class", "com.mysql.cj.jdbc.Driver")
            .put("user", "root")
            .put("password", "123456")
    )
    jdbcClient.getConnection { res ->
        if (res.succeeded()) {
            val connection = res.result()
            val params = JsonArray().add("lisi").add(123)

            connection.queryWithParams("select id from kotlin where name=? and password=?", params) { queryResult ->
                if (queryResult.succeeded()) {
                    val result = queryResult.result()
                    println("成功")
                } else {
                    val error = queryResult.cause()
                    println("失败")
                }

                // 关闭连接
                connection.close()
            }
        } else {
            val error = res.cause()
            println("连接失败")
        }
    }
}

后面该搞ktor了

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