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

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

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

Router.router介绍

[英]Create a router
[中]创建路由器

代码示例

代码示例来源:origin: GoogleContainerTools/jib

@Override
public void start(Future<Void> startFuture) {
  Router router = Router.router(vertx);
  router.get("/").handler(this::hello);
  router.get("/time").handler(this::now);
  vertx.createHttpServer()
      .requestHandler(router)
      .listen(8080, asyncStart -> {
        if (asyncStart.succeeded()) {
          startFuture.complete();
          logger.info("HTTP server running on port 8080");
        } else {
          logger.error("Woops", asyncStart.cause());
          startFuture.fail(asyncStart.cause());
        }
      });
}

代码示例来源: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() {
 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-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() 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-examples

@Override
public void start() {
 Router router = Router.router(vertx);
 router.get("/").handler(this::invoke);
 // Retrieve the service discovery
 ServiceDiscovery.create(vertx, discovery ->
  // Retrieve a web client
  HttpEndpoint.getWebClient(discovery, svc -> svc.getName().equals("vertx-greeting"), ar -> {
   if (ar.failed()) {
    System.out.println("D'oh the service is not available");
   } else {
    client = ar.result();
    vertx.createHttpServer().requestHandler(router).listen(8080);
   }
  }));
}

代码示例来源: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

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() 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 {

  Router router = Router.router(vertx);

  // Allow events for the designated addresses in/out of the event bus bridge
  BridgeOptions opts = new BridgeOptions()
    .addOutboundPermitted(new PermittedOptions().setAddress("feed"));

  // Create the event bus bridge and add it to the router.
  SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
  router.route("/eventbus/*").handler(ebHandler);

  // Start the web server and tell it to use the router to handle requests.
  vertx.createHttpServer().requestHandler(router).listen(8080);

  EventBus eb = vertx.eventBus();

  vertx.setPeriodic(1000l, t -> {
   // Create a timestamp string
   String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));

   eb.send("feed", new JsonObject().put("now", timestamp));
  });
 }
}

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

@Override
 public void start() throws Exception {

  Router router = Router.router(vertx);

  // Allow events for the designated addresses in/out of the event bus bridge
  BridgeOptions opts = new BridgeOptions()
   .addOutboundPermitted(new PermittedOptions().setAddress("feed"));

  // Create the event bus bridge and add it to the router.
  SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
  router.route("/eventbus/*").handler(ebHandler);

  // Create a router endpoint for the static content.
  router.route().handler(StaticHandler.create());

  // Start the web server and tell it to use the router to handle requests.
  vertx.createHttpServer().requestHandler(router).listen(8080);

  EventBus eb = vertx.eventBus();

  vertx.setPeriodic(1000l, t -> {
   // Create a timestamp string
   String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));

   eb.send("feed", new JsonObject().put("now", timestamp));
  });
 }
}

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

@Override
 public void start() throws Exception {

  Router router = Router.router(vertx);

  // Allow events for the designated addresses in/out of the event bus bridge
  BridgeOptions opts = new BridgeOptions()
    .addOutboundPermitted(new PermittedOptions().setAddress("feed"));

  // Create the event bus bridge and add it to the router.
  SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
  router.route("/eventbus/*").handler(ebHandler);

  // Create a router endpoint for the static content.
  router.route().handler(StaticHandler.create());

  // Start the web server and tell it to use the router to handle requests.
  vertx.createHttpServer().requestHandler(router).listen(8080);

  EventBus eb = vertx.eventBus();

  vertx.setPeriodic(1000l, t -> {
   // Create a timestamp string
   String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));

   eb.send("feed", new JsonObject().put("now", timestamp));
  });
 }
}

代码示例来源: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-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-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-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-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

@Override
 public void start() throws Exception {

  Router router = Router.router(vertx);

  // Allow events for the designated addresses in/out of the event bus bridge
  BridgeOptions opts = new BridgeOptions()
      .addInboundPermitted(new PermittedOptions().setAddress("chat.message"))
      .addOutboundPermitted(new PermittedOptions().setAddress("chat.message"));

  // Create the event bus bridge and add it to the router.
  SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
  router.route("/eventbus/*").handler(ebHandler);

  // Create a router endpoint for the static content.
  router.route().handler(StaticHandler.create());

  // Start the web server and tell it to use the router to handle requests.
  vertx.createHttpServer().requestHandler(router).listen(8080);
 }
}

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

@Override
 public void start() throws Exception {

  // Create the client object
  MyService service = new MyServiceImpl();

  // Register the handler
  new ServiceBinder(vertx)
    .setAddress("proxy.example")
    .register(MyService.class, service);

  Router router = Router.router(vertx);

  BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("proxy.example"));

  router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));

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

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

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

@Override
public void start() throws Exception {
 // Create the client object
 ProcessorService service = new ProcessorServiceImpl();
 // Register the handler
 new ServiceBinder(vertx)
  .setAddress("vertx.processor")
  .register(ProcessorService.class, service);
 Router router = Router.router(vertx);
 // Allow events for the designated addresses in/out of the event bus bridge
 BridgeOptions opts = new BridgeOptions()
   .addInboundPermitted(new PermittedOptions().setAddress("vertx.processor"))
   .addOutboundPermitted(new PermittedOptions().setAddress("vertx.processor"));
 // Create the event bus bridge and add it to the router.
 SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
 
 router.route("/eventbus/*").handler(ebHandler);
 router.route().handler(StaticHandler.create());
 //
 vertx.createHttpServer().requestHandler(router).listen(8080);
}

相关文章