gpt4 book ai didi

java - 休息服务调用未触发处理程序

转载 作者:行者123 更新时间:2023-12-05 07:03:53 26 4
gpt4 key购买 nike

我正在用 vert.x 和 Java 编写应用程序。

我有一个类(class),我在其中注册了基本休息服务的终点:

private void setRoutes(Router router){
router.route("/*").handler(StaticHandler.create());
router.get("/service").handler(req -> {
getServices();//Call to Database and populate the services
List<JsonObject> jsonServices = services
.entrySet()
.stream()
.map(service ->
new JsonObject()
.put("name", service.getKey())
.put("status", service.getValue()))
.collect(Collectors.toList());
req.response().setStatusCode(200)
.putHeader("content-type", "application/json")
.end(new JsonArray(jsonServices).encode());
});
router.post("/service").handler(req -> {
JsonObject jsonBody = req.getBodyAsJson();
addService(jsonBody);//Persist the service
//services.put(jsonBody.getString("url"), "UNKNOWN");
req.response()
.putHeader("content-type", "text/plain")
.end("OK");
});

我正在对 GET/service 端点进行 HTTP Get 调用,如下所示,我正在尝试获取响应状态代码。但是每次线程都卡在 conn.getResponseCode() 上,然后什么也没有发生。

我的 router.get("/service").handler 也从未被调用,在 Debug模式下我可以看到 ResponseCode 的值为 -1。从 postman 那里,当我点击这个 url 时,我能够得到正确的结果,而且从浏览器中我也能得到正确的结果。为什么不返回状态代码 200。它也不会进入 catch 或 finally block 。

private void checkService(String key,DBConnector connector) {



// TODO Auto-generated method stub
try {
URL url = new URL("http://localhost:8080/service");
System.out.println(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(50);
conn.connect();


if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
//Update the status of the URL to OK
connector.query("UPDATE service set status = 'OK' where name = ?",new JsonArray().add(key)).setHandler(res -> {
if (res.succeeded()) {
System.out.println("Status updated to OK");
}

else {
//Handle this properly later
System.out.println("Failed to update the status to OK");

}
});
}
else {
connector.query("UPDATE service set status = 'FAIL' where name = ?",new JsonArray().add(key)).setHandler(res -> {
if (res.succeeded()) {
System.out.println("Status updated to fail");
}

else {
//Handle this properly later
System.out.println("Failed to update the status to fail");

}
});
}

}
catch (Exception e) {
e.printStackTrace();
//Code to set the status to FAIL
connector.query("UPDATE service set status = 'FAIL' where name = ?",new JsonArray().add(key)).setHandler(res -> {
if (res.succeeded()) {
System.out.println("Status updated to fail");
}

else {
//Handle this properly later
System.out.println("Failed to update the status to fail");

}
});
}
finally {
System.out.println("INSIDE FINALLY");
}

System.out.println(" Done");

}

最佳答案

尝试在路由后设置静态处理程序:

router.get("/service").handler(req -> {...});

router.post("/service").handler(req -> {...});

router.route("/*").handler(StaticHandler.create());

Vertx 路由器按照它们附加的顺序匹配路由。在您当前的状态下,所有匹配 /* 的请求都将包括 /service 匹配并传递给静态处理程序。

https://vertx.io/docs/vertx-web/java/#_route_order

By default routes are matched in the order they are added to therouter.

When a request arrives the router will step through each route andcheck if it matches, if it matches then the handler for that routewill be called.

If the handler subsequently calls next the handler for the nextmatching route (if any) will be called. And so on.

关于java - 休息服务调用未触发处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63089159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com