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

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

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

Router.routeWithRegex介绍

[英]Add a route that matches the specified HTTP method and path regex
[中]添加与指定HTTP方法和路径正则表达式匹配的路由

代码示例

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
public void init(Router router) {
 router.routeWithRegex(PATTERN_ANY).handler(CookieHandler.create());
 router.routeWithRegex(PATTERN_ANY).handler(createBodyHandler());
 router.routeWithRegex(PATTERN_ANY).failureHandler(this::onFailure).handler(this::onRequest);
}

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
public void init(Router router) {
 prefix = DynamicPropertyFactory.getInstance().getStringProperty(KEY_PREFIX, "api").get();
 withVersion = DynamicPropertyFactory.getInstance().getBooleanProperty(KEY_WITH_VERSION, true).get();
 prefixSegmentCount = DynamicPropertyFactory.getInstance().getIntProperty(KEY_PREFIX_SEGMENT_COUNT, 1).get();
 String regex;
 if (withVersion) {
  regex = "/" + prefix + "/([^\\\\/]+)/([^\\\\/]+)/(.*)";
 } else {
  regex = "/" + prefix + "/([^\\\\/]+)/(.*)";
 }
 router.routeWithRegex(regex).handler(CookieHandler.create());
 router.routeWithRegex(regex).handler(createBodyHandler());
 router.routeWithRegex(regex).failureHandler(this::onFailure).handler(this::onRequest);
}

代码示例来源:origin: xenv/gushici

@Override
public void start(Future<Void> startFuture) {
  Router router = Router.router(vertx);
  router.route().handler(BodyHandler.create());
  router.get("/*").handler(this::log); // 全局日志处理,会执行 next() 到下一个
  router.get("/").handler(this::handleRoot); // 首页
  router.get("/favicon.ico").handler(c -> c.fail(404)); // 针对浏览器返回404
  router.get("/log").handler(this::showLog); // 显示日志
  router.routeWithRegex("/([a-z0-9/]*)\\.?(txt|json|png|svg|)")
    .handler(this::handleGushici); // 核心API调用
  router.route().last().handler(c -> c.fail(404)) // 其他返回404
    .failureHandler(this::returnError); // 对上面所有的错误进行处理
  vertx
    .createHttpServer()
    .requestHandler(router::accept)
    .listen(
      config().getInteger("http.port", 8080),
      result -> {
        if (result.succeeded()) {
          startFuture.complete();
        } else {
          startFuture.fail(result.cause());
        }
      }
    );
}

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

@Test
public void testRegex3() throws Exception {
 router.routeWithRegex(".*foo.txt").handler(rc -> rc.response().setStatusMessage("ok").end());
 testPattern("/dog/cat/foo.txt", "ok");
 testRequest(HttpMethod.POST, "/dog/cat/foo.bar", 404, "Not Found");
}

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

@Test
public void testRegexInSubRouter() throws Exception {
 Router subRouter = Router.router(vertx);
 router.mountSubRouter("/api", subRouter);
 subRouter.routeWithRegex("\\/test").handler(rc -> rc.response().setStatusMessage("sausages").end());
 testRequest(HttpMethod.GET, "/api/test", 200, "sausages");
}

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

@Test
public void testRegex2() throws Exception {
 router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)/blah").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat/blah", "dogcat");
}

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

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

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

@Test
public void testRegexWithNamedParams() throws Exception {
 router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("name") + params.get("surname")).end();
 });
 testPattern("/joe/doe", "joedoe");
}

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

@Test
public void testRegexWithNamedParamsKeepsIndexedParams() throws Exception {
 router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/joe/doe", "joedoe");
}

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

@Test
public void testRegex1() throws Exception {
 router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat", "dogcat");
}

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

WebSocketTransport(Vertx vertx,
          Router router, LocalMap<String, SockJSSession> sessions,
          SockJSHandlerOptions options,
          Handler<SockJSSocket> sockHandler) {
 super(vertx, sessions, options);
 String wsRE = COMMON_PATH_ELEMENT_RE + "websocket";
 router.getWithRegex(wsRE).handler(rc -> {
  HttpServerRequest req = rc.request();
  String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
  if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
   rc.response().setStatusCode(400);
   rc.response().end("Can \"Upgrade\" only to \"WebSocket\".");
  } else {
   ServerWebSocket ws = rc.request().upgrade();
   if (log.isTraceEnabled()) log.trace("WS, handler");
   SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler);
   session.register(req, new WebSocketListener(ws, session));
  }
 });
 router.getWithRegex(wsRE).handler(rc -> {
  if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri());
  rc.response().setStatusCode(400);
  rc.response().end("Can \"Upgrade\" only to \"WebSocket\".");
 });
 router.routeWithRegex(wsRE).handler(rc -> {
  if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri());
  rc.response().putHeader("Allow", "GET").setStatusCode(405).end();
 });
}

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

@Test
public void testRegex1WithMethod() throws Exception {
 router.routeWithRegex(HttpMethod.GET, "\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat", "dogcat");
 testRequest(HttpMethod.POST, "/dog/cat", 404, "Not Found");
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Add a route that matches the specified HTTP method and path regex
 * @param method the HTTP method to match
 * @param regex URI paths that begin with a match for this regex will match
 * @return the route
 */
public io.vertx.rxjava.ext.web.Route routeWithRegex(HttpMethod method, String regex) { 
 io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.routeWithRegex(method, regex));
 return ret;
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegex3() throws Exception {
 router.routeWithRegex(".*foo.txt").handler(rc -> rc.response().setStatusMessage("ok").end());
 testPattern("/dog/cat/foo.txt", "ok");
 testRequest(HttpMethod.POST, "/dog/cat/foo.bar", 404, "Not Found");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegexInSubRouter() throws Exception {
 Router subRouter = Router.router(vertx);
 router.mountSubRouter("/api", subRouter);
 subRouter.routeWithRegex("\\/test").handler(rc -> rc.response().setStatusMessage("sausages").end());
 testRequest(HttpMethod.GET, "/api/test", 200, "sausages");
}

代码示例来源:origin: io.vertx/vertx-web

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

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegex2() throws Exception {
 router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)/blah").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat/blah", "dogcat");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegexWithNamedParams() throws Exception {
 router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("name") + params.get("surname")).end();
 });
 testPattern("/joe/doe", "joedoe");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegex1() throws Exception {
 router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat", "dogcat");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testRegex1WithMethod() throws Exception {
 router.routeWithRegex(HttpMethod.GET, "\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat", "dogcat");
 testRequest(HttpMethod.POST, "/dog/cat", 404, "Not Found");
}

相关文章