Capturing path parameters with regular expressions

当使用正则表达式的时候,你还可以捕获路径参数:

  1. Route route = router.routeWithRegex(".*foo");
  2. // This regular expression matches paths that start with something like:
  3. // "/foo/bar" - where the "foo" is captured into param0 and the "bar" is captured into
  4. // param1
  5. route.pathRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(routingContext -> {
  6. String productType = routingContext.request().getParam("param0");
  7. String productID = routingContext.request().getParam("param1");
  8. // Do something with them...
  9. });

在上面的例子中,如果请求路径是/tools/drill123/, 那么我们设置的route会被匹配到, 然后productType会接收到参数值tools, productID会接收到参数值drill123.

Captures are denoted in regular expressions with capture groups (i.e. surrounding the capture with round brackets)