Rest service call is not triggering the handler

Viewed 124

I am writing an application in vert.x and Java.

I have a class in which I have registered the end points for basic rest services:

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

I am making a HTTP Get call to the GET /service end point as shown below and I am trying to get the response status code. But every-time the thread just gets stuck at the conn.getResponseCode() and then nothing happens.

Also my router.get("/service").handler is never called and in debug mode I can see that ResponseCode has a value of -1. From postman when I hit this url I am able to get proper results and also from the browser I can get proper results. Why is status code 200 not being returned. Also it does not go the catch or the finally blocks.

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

}
1 Answers

Try setting the static handler after your routes:

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

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

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

Vertx routers match routes in the order they were attached in. In your current state all requests matching /* which would include /service are matched and passed to the static handler.

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

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

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

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

Related