private void now(RoutingContext context) { logger.info("Time request from {}", context.request().remoteAddress()); JsonObject data = new JsonObject() .put("powered-by", "vertx") .put("current-time", System.currentTimeMillis()); context.response() .putHeader("Content-Type", "application/json") .end(data.encode()); } }
routingContext.response().setStatusMessage("OK").end(); }); if (failure instanceof ValidationException) routingContext.response() .setStatusCode(400) .setStatusMessage("ValidationException thrown! " + ((ValidationException) failure).type().name()) .end(); });
@Test public void testPathParamsAreFulfilled() throws Exception { router.route("/blah/:abc/quux/:def/eep/:ghi").handler(rc -> { Map<String, String> params = rc.pathParams(); rc.response().setStatusMessage(params.get("abc") + params.get("def") + params.get("ghi")).end(); }); testPattern("/blah/tim/quux/julien/eep/nick", "timjuliennick"); }
@Test public void testSubRouteRegex() throws Exception { Router subRouter = Router.router(vertx); router.routeWithRegex("/foo/.*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); subRouter.route("/blah").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/foo/blah", 500, "Internal Server Error"); }
public static void main(String[] args) { Vertx vertx = Vertx.vertx(); Router router = Router.router(vertx); router.get("/").handler(rc -> rc.response().end("Hello")); router.get("/:name").handler(rc -> rc.response().end("Hello " + rc.pathParam("name"))); vertx.createHttpServer() .requestHandler(router) .listen(8080); } }
private void invoke(RoutingContext rc) { client.get("/luke").as(BodyCodec.string()).send(ar -> { if (ar.failed()) { rc.response().end("Unable to call the greeting service: " + ar.cause().getMessage()); } else { if (ar.result().statusCode() != 200) { rc.response().end("Unable to call the greeting service - received status:" + ar.result().statusMessage()); } else { rc.response().end("Greeting service invoked: '" + ar.result().body() + "'"); } } }); } }
public static void main(String[] args) { // Create the Vert.x instance Vertx vertx = Vertx.vertx(); // Create a router Router router = Router.router(vertx); router.get("/").handler(rc -> rc.response().end("Hello")); router.get("/:name").handler(rc -> rc.response().end("Hello " + rc.pathParam("name"))); // Start the HTTP server. vertx.createHttpServer() .requestHandler(router) .listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().blockingHandler(routingContext -> { // Blocking handlers are allowed to block the calling thread // So let's simulate a blocking action or long running operation try { Thread.sleep(5000); } catch (Exception ignore) { } // Now call the next handler routingContext.next(); }, false); router.route().handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end("Hello World!"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Test public void testFailureinHandlingFailureWithInvalidStatusMessage() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusMessage("Hello\nWorld").end()); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
router.route().handler(BodyHandler.create()); router.route("/").handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end( "<form action=\"/form\" method=\"post\">\n" + " <div>\n" + router.post("/form").handler(ctx -> { ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain"); ctx.response().end("Hello " + ctx.request().getParam("name") + "!"); });
private void handleListProducts(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); conn.query("SELECT id, name, price, weight FROM products", query -> { if (query.failed()) { sendError(500, response); } else { JsonArray arr = new JsonArray(); query.result().getRows().forEach(arr::add); routingContext.response().putHeader("content-type", "application/json").end(arr.encode()); } }); }
@Test public void testProducesTopLevelTypeWildcard() throws Exception { router.route().produces("application/json").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "*/json", 200, "application/json"); testRequestWithAccepts(HttpMethod.GET, "/foo", "*/html", 404, "Not Found"); }