io.vertx.ext.web.Route.handler()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中io.vertx.ext.web.Route.handler方法的一些代码示例,展示了Route.handler的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Route.handler方法的具体详情如下:
包路径:io.vertx.ext.web.Route
类名称:Route
方法名:handler

Route.handler介绍

[英]Append a request handler to the route handlers list. The router routes requests to handlers depending on whether the various criteria such as method, path, etc match. When method, path, etc are the same for different routes, You should add multiple handlers to the same route object rather than creating two different routes objects with one handler for route
[中]将请求处理程序追加到路由处理程序列表。路由器根据方法、路径等各种条件是否匹配,将请求路由到处理程序。当不同路由的方法、路径等相同时,应向同一路由对象添加多个处理程序,而不是使用一个处理程序为路由创建两个不同的路由对象

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  Router router = Router.router(vertx);

  router.route().handler(routingContext -> {
   routingContext.response().putHeader("content-type", "text/html").end("Hello World!");
  });

  vertx.createHttpServer().requestHandler(router).listen(8080);
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
public void start() {
 Router router = Router.router(vertx);
 // Serve the static pages
 router.route().handler(StaticHandler.create());
 vertx.createHttpServer().requestHandler(router).listen(8080);
 System.out.println("Server is started");
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testFailureUsingInvalidCharsInStatus() throws Exception {
 String path = "/blah";
 router.route(path).handler(rc -> rc.response().setStatusMessage("Hello\nWorld!").end());
 testRequest(HttpMethod.GET, path, 500, "Internal Server Error");
}

代码示例来源:origin: vert-x3/vertx-web

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

代码示例来源:origin: vert-x3/vertx-examples

@Override
  public void start() {
   Router router = Router.router(vertx);
   router.get("/").handler(rc -> rc.response().end(UUID.randomUUID().toString()));

   vertx.createHttpServer()
    .requestHandler(router)
    .listen(8080);
  }
}

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-web

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {
  Router router = Router.router(vertx);

  // Serve the static pages
  router.route().handler(StaticHandler.create());

  vertx.createHttpServer().requestHandler(router).listen(configuration.httpPort());
 }
}

代码示例来源:origin: vert-x3/vertx-web

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-web

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-web

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-examples

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

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testProducesAll2() throws Exception {
 router.route().produces("application/json").handler(rc -> {
  rc.response().setStatusMessage(rc.getAcceptableContentType());
  rc.response().end();
 });
 testRequestWithAccepts(HttpMethod.GET, "/foo", "*", 200, "application/json");
}

代码示例来源:origin: vert-x3/vertx-examples

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

相关文章