0

我正在尝试使用 Vertx 的 WebClient 测试我的 Post 端点,并始终将 500 作为状态代码。谁能让我知道我在这里做错了什么:-

final String jsonBody = "{\"url\": \"https://www.google.se\"}";
    WebClient.create(vertx)
            .post(8080, "::1", "/service")
            .sendJson(
                jsonBody,
                response ->
                    testContext.verify(
                        () -> {
                          System.out.println(response.result().statusCode());
                          assertEquals("OK", response.result());
                        }));
4

2 回答 2

0

500 is an internal server error. It is not getting the required data in your case..I guess. So try to send the payload using

  • Convert string to jsonobject and send usig sendJsonObject method
  • Convert buffer to jsonobject and send usig sendBuffer method
于 2020-05-07T03:34:56.240 回答
0

这应该适合你。我展示了 Vert.x 客户端和处理程序。JsonObject客户端从字符串创建一个。处理程序在服务器中。

@Test
public void testPostURL(TestContext context) {
    Async async = context.async();
    final String body = "{\"url\": \"https://www.google.se\"}";
    WebClient.create(vertx)
            .post(8080, "localhost", "/service")
            .putHeader("content-type", "application/json")
            .sendJson( new JsonObject(body),
                     requestResponse -> {
                        context.assertEquals(requestResponse.result().statusCode(), 200);
                        async.complete();
                    });
}

处理程序需要一个 JsonObject 并返回 url ( https://www.google.se )

    private void service(RoutingContext rc) {
    HttpServerResponse response = rc.response();
    JsonObject body = rc.getBodyAsJson();
    String site = body.getString("url");
    response.setStatusCode(200)
            .putHeader("content-type", "application/json; charset=utf-8")
            .end(Json.encodePrettily(site));
}
于 2020-05-20T10:11:40.087 回答