@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"); }
@Override public void start() { setUpInitialData(); Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.get("/products/:productID").handler(this::handleGetProduct); router.put("/products/:productID").handler(this::handleAddProduct); router.get("/products").handler(this::handleListProducts); 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"); }
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); } }
@Test public void testFailureHandlerNoMatch() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/other").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); // Default failure response testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
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() { Router router = Router.router(vertx); // Serve the dynamic pages router.route("/dynamic/*") .handler(ctx -> { // put the context into the template render context ctx.put("context", ctx); ctx.next(); }) .handler(TemplateHandler.create(MVELTemplateEngine.create(vertx))); // Serve the static pages router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); }
@Test public void testFailureHandler2() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/bl*").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); router.route().handler(routingContext -> { Session session = routingContext.session(); Integer cnt = session.get("hitcount"); cnt = (cnt == null ? 0 : cnt) + 1; session.put("hitcount", cnt); routingContext.response().putHeader("content-type", "text/html") .end("<html><body><h1>Hitcount: " + cnt + "</h1></body></html>"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Validate public void start() throws Exception { setUpInitialData(); TcclSwitch.executeWithTCCLSwitch(() -> { Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.get("/products/:productID").handler(this::handleGetProduct); router.put("/products/:productID").handler(this::handleAddProduct); router.get("/products").handler(this::handleListProducts); router.get("/assets/*").handler(StaticHandler.create("assets", this.getClass().getClassLoader())); LOGGER.info("Creating HTTP server for vert.x web application"); HttpServer server = vertx.createHttpServer(); server.requestHandler(router).listen(8081); }); }
@Test public void testFailureHandler1() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@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); } }
@Override public void start() throws Exception { final Router router = Router.router(vertx); // Populate context with data router.route().handler(ctx -> { ctx.put("title", "Vert.x Web Example Using Rocker"); ctx.put("name", "Rocker"); ctx.next(); }); // Render a custom template. // Note: you need a compile-time generator for Rocker to work properly // See the pom.xml for an example router.route().handler(TemplateHandler.create(RockerTemplateEngine.create())); vertx.createHttpServer().requestHandler(router).listen(8080); } }
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") + "!"); });