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

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

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

Router.getWithRegex介绍

[英]Add a route that matches a HTTP GET request and the specified path regex
[中]添加与HTTP GET请求和指定路径regex匹配的路由

代码示例

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

@Test
public void testSetRegexGroupsNamesMethod() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("hello");
 Route route1 = router.getWithRegex("\\/(?<p0>[a-z]{2})");
 route1.setRegexGroupsNames(groupNames);
 route1.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("hello"))
  .end());
 testRequest(HttpMethod.GET, "/hi", 200, "hi");
}

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

@Test
public void testRegexGroupsNamesWithMethodOverride() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("FirstParam");
 groupNames.add("SecondParam");
 Route route = router.getWithRegex("\\/([a-z]{2})([a-z]{2})");
 route.setRegexGroupsNames(groupNames);
 route.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("FirstParam") + "-" + routingContext.pathParam("SecondParam"))
  .end());
 testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb");
}

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

@Test
public void testSetRegexGroupsNamesMethodWithUnorderedGroups() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("firstParam");
 groupNames.add("secondParam");
 Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2})(?<p0>[a-z]{2})");
 route1.setRegexGroupsNames(groupNames);
 route1.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bb");
}

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

@Test
public void testSetRegexGroupsNamesMethodWithNestedRegex() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("firstParam");
 groupNames.add("secondParam");
 Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2}(?<p0>[a-z]{2}))");
 route1.setRegexGroupsNames(groupNames);
 route1.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa");
}

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

router.getWithRegex("\\/iframe-[^\\/]*\\.html").handler(iframeHandler);

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

HtmlFileTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
         Handler<SockJSSocket> sockHandler) {
 super(vertx, sessions, options);
 String htmlFileRE = COMMON_PATH_ELEMENT_RE + "htmlfile.*";
 router.getWithRegex(htmlFileRE).handler(rc -> {
  if (log.isTraceEnabled()) log.trace("HtmlFile, get: " + rc.request().uri());
  String callback = rc.request().getParam("callback");
  if (callback == null) {
   callback = rc.request().getParam("c");
   if (callback == null) {
    rc.response().setStatusCode(500).end("\"callback\" parameter required\n");
    return;
   }
  }
  if (CALLBACK_VALIDATION.matcher(callback).find()) {
   rc.response().setStatusCode(500);
   rc.response().end("invalid \"callback\" parameter\n");
   return;
  }
  HttpServerRequest req = rc.request();
  String sessionID = req.params().get("param0");
  SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
  session.register(req, new HtmlFileListener(options.getMaxBytesStreaming(), rc, callback, session));
 });
}

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

@Test
public void testRegexGroupsNames() throws Exception {
 router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb");
}

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

@Test
public void testRegexGroupsNamesWithNestedGroups() throws Exception {
 router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa");
}

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

EventSourceTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
           Handler<SockJSSocket> sockHandler) {
 super(vertx, sessions, options);
 String eventSourceRE = COMMON_PATH_ELEMENT_RE + "eventsource";
 router.getWithRegex(eventSourceRE).handler(rc -> {
  if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + rc.request().uri());
  String sessionID = rc.request().getParam("param0");
  SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
  HttpServerRequest req = rc.request();
  session.register(req, new EventSourceListener(options.getMaxBytesStreaming(), rc, session));
 });
}

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

@Test
public void testGetWithRegex() throws Exception {
 router.getWithRegex("\\/somepath\\/.*").handler(rc -> rc.response().setStatusMessage("foo").end());
 testRequest(HttpMethod.GET, "/somepath/whatever", 200, "foo");
 testRequest(HttpMethod.GET, "/otherpath/whatever", 404, "Not Found");
 testRequest(HttpMethod.POST, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.PUT, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.DELETE, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.OPTIONS, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.HEAD, "/somepath/whatever", 404, "Not Found");
}

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

router.getWithRegex(jsonpRE).handler(rc -> {
 if (log.isTraceEnabled()) log.trace("JsonP, get: " + rc.request().uri());
 String callback = rc.request().getParam("callback");

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

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

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

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

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

@Test
public void testRegexGroupsNamesWithMethodOverride() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("FirstParam");
 groupNames.add("SecondParam");
 Route route = router.getWithRegex("\\/([a-z]{2})([a-z]{2})");
 route.setRegexGroupsNames(groupNames);
 route.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("FirstParam") + "-" + routingContext.pathParam("SecondParam"))
  .end());
 testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb");
}

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

@Test
public void testSetRegexGroupsNamesMethodWithNestedRegex() throws Exception {
 List<String> groupNames = new ArrayList<>();
 groupNames.add("firstParam");
 groupNames.add("secondParam");
 Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2}(?<p0>[a-z]{2}))");
 route1.setRegexGroupsNames(groupNames);
 route1.handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa");
}

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

@Test
public void testRegexGroupsNamesWithNestedGroups() throws Exception {
 router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa");
}

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

@Test
public void testRegexGroupsNames() throws Exception {
 router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext
  .response()
  .setStatusCode(200)
  .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam"))
  .end());
 testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb");
}

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

EventSourceTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
           Handler<SockJSSocket> sockHandler) {
 super(vertx, sessions, options);
 String eventSourceRE = COMMON_PATH_ELEMENT_RE + "eventsource";
 router.getWithRegex(eventSourceRE).handler(rc -> {
  if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + rc.request().uri());
  String sessionID = rc.request().getParam("param0");
  SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
  HttpServerRequest req = rc.request();
  session.register(req, new EventSourceListener(options.getMaxBytesStreaming(), rc, session));
 });
}

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

@Test
public void testGetWithRegex() throws Exception {
 router.getWithRegex("\\/somepath\\/.*").handler(rc -> rc.response().setStatusMessage("foo").end());
 testRequest(HttpMethod.GET, "/somepath/whatever", 200, "foo");
 testRequest(HttpMethod.GET, "/otherpath/whatever", 404, "Not Found");
 testRequest(HttpMethod.POST, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.PUT, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.DELETE, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.OPTIONS, "/somepath/whatever", 404, "Not Found");
 testRequest(HttpMethod.HEAD, "/somepath/whatever", 404, "Not Found");
}

相关文章