Vert.x系列04 - 你意想不到的那些路由规则
简介
Vert.x 的路由规则
Vert.x的路由规则主要有以下几种: 
 - 通过指定的路径进行路由 
- 正则匹配的路由 
- 根据请求方法进行路由 
- 根据请求的 - MIME类型进行路由
- 根据客户端可接受的 - MIME类型进行路由
  
    
    
  
   
     
     
   HttpServer httpServer = vertx.createHttpServer();
   
     
     
   
   
     
     
   
Router router = Router.router(vertx);
   
     
     
   
   
     
     
   
   
     
     
   // 只会捕获到 /fxb/path 这个路由
   
     
     
   
router.route().path(
   
     
     
   "/fxb/path").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "fxb/path");
   
     
     
   
});
   
     
     
   
   
     
     
   // 获取以action结尾的路由
   
     
     
   
router.route().pathRegex(
   
     
     
   ".*action").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   ".*action");
   
     
     
   
});
   
     
     
   
   
     
     
   // 捕获以 "/fxb/path/"开头的路由
   
     
     
   
router.route().path(
   
     
     
   "/fxb/path/*").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "/fxb/path/*");
   
     
     
   
});
   
     
     
   
   
     
     
   // 获取 正则匹配路由的中的路由参数 - 给分组命名
   
     
     
   
router.routeWithRegex(
   
     
     
   "\\/(?<id>[^\\/]+)\\/(?<name>[^\\/]+)").handler(routingContext -> {
   
     
     
   
    String id = routingContext.request().getParam(
   
     
     
   "id");
   
     
     
   
    String name = routingContext.request().getParam(
   
     
     
   "name");
   
     
     
   
    String format = String.format(
   
     
     
   "fxb/param/路由, 获取到的参数: id=[%s],name=[%s]", id, name);
   
     
     
   
    routingContext.response().end(format);
   
     
     
   
});
   
     
     
   
   
     
     
   // 只接收POST请求
   
     
     
   
router.route(HttpMethod.POST, 
   
     
     
   "/fxb/method/*").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "/fxb/method/*");
   
     
     
   
});
   
     
     
   
   
     
     
   // 基于Request的MIME类型的路由(consumes可以指定多个,也支持*/json这种格式)
   
     
     
   
router.route().consumes(
   
     
     
   "text/html/").path(
   
     
     
   "/fxb/mime/consumes/").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "/fxb/mime/consumes/的text/html");
   
     
     
   
});
   
     
     
   
   
     
     
   
   
     
     
   // 基于客户端可接受的MIME类型的路由
   
     
     
   
router.route().produces(
   
     
     
   "text/html/").path(
   
     
     
   "/fxb/mime/produces/").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "/fxb/mime/produces/的text/html");
   
     
     
   
});
   
     
     
   
   
     
     
   
   
     
     
   /// 以上的路由方式可随意结合使用
   
     
     
   
httpServer.requestHandler(router).listen(
   
     
     
   8080);
   
     
     
   
  
    
    
    
  
  
    
    
  
   
     
     
   // 获取路由中的参数
   
     
     
   
router.route().path(
   
     
     
   "/fxb/param/:id/:name").handler(routingContext -> {
   
     
     
   
    String id = routingContext.request().getParam(
   
     
     
   "id");
   
     
     
   
    String name = routingContext.request().getParam(
   
     
     
   "name");
   
     
     
   
    String format = String.format(
   
     
     
   "fxb/param/路由, 获取到的参数: id=[%s],name=[%s]", id, name);
   
     
     
   
    routingContext.response().end(format);
   
     
     
   
});
   
     
     
   
   
     
     
   // 获取 正则匹配路由的中的路由参数
   
     
     
   
router.route().pathRegex(
   
     
     
   "*.action").pathRegex(
   
     
     
   "\\/([^\\/]+)\\/([^\\/]+)").handler(routingContext -> {
   
     
     
   
    String id = routingContext.request().getParam(
   
     
     
   "param0");
   
     
     
   
    String name = routingContext.request().getParam(
   
     
     
   "param1");
   
     
     
   
    String format = String.format(
   
     
     
   "fxb/param/路由, 获取到的参数: id=[%s],name=[%s]", id, name);
   
     
     
   
    routingContext.response().end(format);
   
     
     
   
});
   
     
     
   
  
    
    
    
  
  
 
 
  
    
    
  
   
     
     
   Router router = Router.router(vertx);
   
     
     
   
   
     
     
   
router.route(
   
     
     
   "/some/path/").handler(routingContext -> {
   
     
     
   
    routingContext.response().write(
   
     
     
   "handler1");
   
     
     
   
    routingContext.next();
   
     
     
   
});
   
     
     
   
router.route(
   
     
     
   "/some/path/").handler(routingContext -> {
   
     
     
   
    routingContext.response().setChunked(
   
     
     
   true).write(
   
     
     
   "handler2");
   
     
     
   
    routingContext.next();
   
     
     
   
});
   
     
     
   
   
     
     
   
router.route(
   
     
     
   "/some/path/").handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "handler3");
   
     
     
   
});
   
     
     
   
   
     
     
   
vertx.createHttpServer().requestHandler(router).listen(
   
     
     
   8080);
   
     
     
   
  
    
    
    
 
  
    
    
  
   
     
     
   Router router = Router.router(vertx);
   
     
     
   
   
     
     
   
router.route(
   
     
     
   "/some/path/").order(
   
     
     
   1).handler(routingContext -> {
   
     
     
   
    routingContext.response().setChunked(
   
     
     
   true).write(
   
     
     
   "handler1");
   
     
     
   
    routingContext.next();
   
     
     
   
});
   
     
     
   
router.route(
   
     
     
   "/some/path/").order(
   
     
     
   0).handler(routingContext -> {
   
     
     
   
    routingContext.response().write(
   
     
     
   "handler2");
   
     
     
   
    routingContext.next();
   
     
     
   
});
   
     
     
   
   
     
     
   
router.route(
   
     
     
   "/some/path/").order(
   
     
     
   2).handler(routingContext -> {
   
     
     
   
    routingContext.response().end(
   
     
     
   "handler3");
   
     
     
   
});
   
     
     
   
   
     
     
   
vertx.createHttpServer().requestHandler(router).listen(
   
     
     
   8080);
   
     
     
   
  
    
    
    
 其他功能
启动禁用路由
router 对象有一个属性是 
  order, 我们可以通过该属性来设置这条路由是否生效. 如下。 
 
  
    
    
  
   
     
     
   router.route(
   
     
     
   "/some/path/").order(
   
     
     
   1).handler(routingContext -> {
   
     
     
   
    routingContext.response().write(
   
     
     
   "handler1");
   
     
     
   
    routingContext.next();
   
     
     
   
}).disable()
   
     
     
   
  
    
    
    
  
  
 
 Route.handler() 这个方法中, 的 
  Handler<RoutingContext> 中的 
  RoutingContext 有一个 
  reroute方法. 
 
  
    
    
  
   
     
     
   router.route(
   
     
     
   "/some/path/").order(
   
     
     
   1).handler(routingContext -> {
   
     
     
   
    routingContext.response().write(
   
     
     
   "handler1");
   
     
     
   
    routingContext.reroute(
   
     
     
   "/some/path2/");
   
     
     
   
});
   
     
     
   
  
    
    
    
 子路由器
  
    
    
  
   
     
     
   Router sonRouter = Router.router(vertx);
   
     
     
   
sonRouter.route(
   
     
     
   "/son").handler(routingContext -> {});
   
     
     
   
   
     
     
   
Router mainRouter = Router.router(vertx);
   
     
     
   
mainRouter.mountSubRouter(
   
     
     
   "/main",sonRouter).route(
   
     
     
   "/main");
   
     
     
   
  
    
    
    
 mountSubRouter 完成了 将 
  sonRouter 作为了 
  mainRouter 的子路由器. 访问子路由器需要 
  /main/son/ 
 小结
- 路由规则 
- 通过指定的路径进行路由 
- 正则匹配的路由 
- 根据请求方法进行路由 
- 根据请求的 - MIME类型进行路由
- 根据客户端可接受的 - MIME类型进行路由
- 获取路由中的参数 
- 普通路由中的参数 
- 正则匹配方式中的参数 
- 路由顺序 
- 其他功能 
- 启用/禁用路由 
- 重新路由 
- 子路由 
