【问题标题】:Rest service call is not triggering the handler休息服务调用未触发处理程序
【发布时间】:2020-11-15 05:45:19
【问题描述】:

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

我有一个班级,我在其中注册了基本休息服务的端点:

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 从未被调用,在调试模式下我可以看到 ResponseCode 的值为 -1。当我点击这个网址时,从邮递员那里我可以获得正确的结果,也可以从浏览器获得正确的结果。为什么没有返回状态码 200。它也不会去 catch 或 finally 块。

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");

}

【问题讨论】:

    标签: java rest http microservices vert.x


    【解决方案1】:

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

    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

    默认情况下,路由按照添加到 路由器。

    当请求到达时,路由器将逐步遍历每条路由并 检查它是否匹配,如果匹配则该路由的处理程序 将被调用。

    如果处理程序随后调用下一个处理程序的下一个 将调用匹配的路线(如果有)。以此类推。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多