完全无法读取vertx路由处理程序链中的post数据 - 尝试各方面都没有成功

问题描述 投票:0回答:1

我正在使用vertx web运行gradle构建。我的库依赖包括

   // for mock API serving https://mvnrepository.com/artifact/io.vertx/vertx-core
    compile group: 'io.vertx', name: 'vertx-core', version: '3.5.3'
    compile group: 'io.vertx', name: 'vertx-web', version: '3.5.3'

所以在我的groovy脚本代码中,我这样做

Vertx vertx = Vertx.vertx()
HttpServer server = vertx.createHttpServer()  
...

然后我声明一个路由来捕获资源上的所有请求并处理请求并形成响应 - 尝试保持简单 - 只需将简单字符串作为响应返回

Router allRouter = Router.router(vertx)

allRouter.route ( "/api/now/table/incident")
        .handler(BodyHandler.create())
        .blockingHandler { routingContext ->

    def request  = routingContext.request()
    HttpMethod method = request.method()

    def response = routingContext.response()
    response.putHeader ("content-type", "text/plain")

    def uri = routingContext.request().absoluteURI()
    switch (method) {
        case HttpMethod.GET:
            println "processing a resource GET on uri : $uri "
            response.end ("(GET) howdi will")
            break

        case HttpMethod.POST:

            String bodyEnc = routingContext.getBodyAsJson().encodePrettily()

            println "processing a resource POST on uri : $uri"

            println "post request received post data : " + bodyEnc

            response.end ("(POST) howdi will")
            break
    }

}

我在路线中的通用处理程序之前为BodyHandling创建了一个处理程序。

然后我用路线启动服务器

server.requestHandler(allRouter.&accept)
server.listen(8081, "localhost")

从邮递员那里得到的请求一切正常。

当我使用带有请求正文数据的发布请求时 - 服务挂起而我必须取消 - 它永远不会进入switch语句。发布帖子时控制台中发生的一切都是

23:30:16.455 [vert.x-eventloop-thread-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 32768
23:30:16.455 [vert.x-eventloop-thread-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
23:30:16.455 [vert.x-eventloop-thread-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
23:30:16.455 [vert.x-eventloop-thread-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
23:30:16.478 [vert.x-eventloop-thread-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
23:30:16.482 [vert.x-eventloop-thread-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@51d486

这是最接近相关主题enter link description here

我现在尝试了无数种方法,当我得到它时,在请求中设置bodyHandler - 无法让它工作。

我的postman帖子看起来像这样 - 标题设置为Content-Type application / json,Content-Length是296字节(utf16中的字节长度),Accept是text / plain接收简单响应

enter image description here文档不太清楚。吹了12个小时试图破解这个。

有没有人知道在使用vertx web时应该如何获取请求的发布数据

groovy vert.x httphandler
1个回答
0
投票

好像我的问题是发送的字节大小。我已经计算了reqBody字符串的字节为296个字节(使用UTF16字符集),并将其设置为postman请求。

所以vertx服务器试图在处理正文时读取许多并挂起。当我刚刚做了reqBody.getBytes()。size - 有效的UTF8时,这返回了147个字节。当我在邮递员中将内容长度更改为此内容时,它开始起作用。

在设置BodyHandler的位置存在时间问题。我试过这是开关盒的情况,并且它已经被读取失败了。

然而,这些中的任何一个似乎都成功

选项1:'在路径(路径)调用之前创建主体处理程序'

allRouter.route().handler(BodyHandler.create())

//now try and setup the route for API path interception
allRouter.route ( "/api/now/table/incident")
        .blockingHandler { routingContext -> ...

选项2:'链接处理程序,但将Body Handler放在第一位'

//now try and setup the route for API path interception, followed by body handler
allRouter.route ( "/api/now/table/incident")
        .handler(BodyHandler.create())
        .blockingHandler { routingContext ->...

感谢Homerman的回复 - 但它设置了内容长度,假设内部字节大小的UTF16是真正的问题。现在回到试图完成我打算做的事情

© www.soinside.com 2019 - 2024. All rights reserved.