簡體   English   中英

Vert.x-從POST請求獲取參數

[英]Vert.x - Get parameters from POST request

我試圖創建一個Vert.x Rest服務,以對某些URL \\分析上的POST請求做出響應。

使用以下命令

curl -D- http:// localhost:8080 \\ analyze -d'{“ text”:“ bla”}'

我想從命令中提取“ bla”並對其執行簡單的文本分析:

這是我的代碼的草稿:

    @Override
public void start(Future<Void> fut) throws Exception {

    router = Router.router(vertx);
    router.post("/analyze").handler(this::analyze);

    // Create Http server and pass the 'accept' method to the request handler
    vertx.createHttpServer().requestHandler(router::accept).
            listen(config().getInteger("http.port", 9000),
                    result -> {
                        if (result.succeeded()) {
                            System.out.println("Http server completed..");
                            fut.complete();
                        } else {
                            fut.fail(result.cause());
                            System.out.println("Http server failed..");
                        }
                    }
            );
}


private void analyze(RoutingContext context) {
    HttpServerResponse response = context.response();
    String bodyAsString = context.getBodyAsString();
    JsonObject body = context.getBodyAsJson();

    if (body == null){
        response.end("The Json body is null. Please recheck.." + System.lineSeparator());
    }
    else
    {
        String postedText = body.getString("text");
        response.setStatusCode(200);
        response.putHeader("content-type", "text/html");
        response.end("you posted json which contains the following " + postedText);
    }

}

}

您知道如何從POST獲取“ bla”嗎?

嘗試以下路由器和處理程序:

Router router = Router.router(vertx);
// add a handler which sets the request body on the RoutingContext.
router.route().handler(BodyHandler.create());
// expose a POST method endpoint on the URI: /analyze
router.post("/analyze").handler(this::analyze);

// handle anything POSTed to /analyze
public void analyze(RoutingContext context) {
    // the POSTed content is available in context.getBodyAsJson()
    JsonObject body = context.getBodyAsJson();

    // a JsonObject wraps a map and it exposes type-aware getters
    String postedText = body.getString("text");

    context.response().end("You POSTed JSON which contains a text attribute with the value: " + postedText);
}

有了上面的代碼,此CURL命令就可以了...

curl -D- http://localhost:9000/analyze -d '{"text":"bla"}'

... 將返回:

$ curl -D- http://localhost:9000/analyze -d '{"text":"bla"}'
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 67
Set-Cookie: vertx-web.session=21ff020c9afa5ec9fd5948acf64c5a85; Path=/

You POSTed JSON which contains a text attribute with the value: bla

查看您的問題,您已經定義了一個名為/analyze的終結點,但是隨后您提出了以下CURL命令: curl -D- http://localhost:8080 -d '{"text":"bla"}' /analyze端點。 也許這是問題的一部分,或者只是准備問題時的錯字。 無論如何,我上面提供的代碼將:

  • http://localhost:9000/analyze定義一個端點
  • 處理發布到該端點的內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM