简体   繁体   English

如何匹配Akka HTTP中的所有路径

[英]How to match all paths in Akka HTTP

Most Akka HTTP examples show it is really easy to define Routes using path . 大多数Akka HTTP示例显示使用path定义Routes非常容易。

I have the following (slightly simplified) from the introductory example , 我从介绍性示例中得到以下(略微简化),

val route =
  path("hello") {
    complete(
      HttpEntity(
        ContentTypes.`text/html(UTF-8)`,
        "<h1>Say hello to akka-http</h1>"))
  }

However, the above only works for "/hello" and I want to match all possible paths or URLs, not just "hello". 但是,上面只适用于“/ hello”,我想匹配所有可能的路径或URL,而不仅仅是“你好”。 The empty string, "" , only matches the root path and "*" matches the literal path "/*". 空字符串""仅匹配根路径, "*"匹配文字路径“/ *”。 The parameter to path is required and cannot simply be omitted. path的参数是必需的,不能简单地省略。

The there are a lot of ways this can be accomplished. 有很多方法可以实现。 I believe the following shows some ways ordered most preferred to least preferred. 我相信以下显示了一些最优先选择的方式。

Use other types of directives 使用其他类型的指令

The simplest solution requires knowing that the Route can be any Directive , not only path directives . 最简单的解决方案需要知道Route可以是任何指令 ,而不仅仅是路径指令 Thus, the original example could be modified to achieve the desired results by just by dropping the path completely: 因此,可以通过完全删除path来修改原始示例以实现所需的结果:

val route =
  complete(
    HttpEntity(
      ContentTypes.`text/html(UTF-8)`,
      "<h1>Say hello to akka-http</h1>"))

Use a different PathMatcher with path 使用带path的其他PathMatcher

path does not actually take a String as a parameter. path实际上并不将String作为参数。 path("hello") is actually pathPrefix(_segmentStringToPathMatcher("hello")) after implicit conversion. path("hello")实际上是隐式转换后的pathPrefix(_segmentStringToPathMatcher("hello")) The desired outcome is possible using different arguments of type PathMatcher . 使用PathMatcher类型的不同参数可以获得所需的结果。

You can use Remaining which matches everything left. 您可以使用Remaining部分。

val route =
  path(Remaining) { _ =>
    complete(
      HttpEntity(
        ContentTypes.`text/html(UTF-8)`,
        "<h1>Say hello to akka-http</h1>"))
  }

Or you can use a regular expression: 或者您可以使用正则表达式:

val route =
  path(".*".r) { _ =>
    complete(
      HttpEntity(
        ContentTypes.`text/html(UTF-8)`,
        "<h1>Say hello to akka-http</h1>"))
  }

Both of the above make available the match and thus you have the additional ignored lambda argument. 以上两个都使匹配成为可能,因此您有另外忽略的lambda参数。

Use pathPrefix("") instead of path("") 使用pathPrefix("")而不是path("")

According to the documentation for path empty string, "" , does in behave a little like a wildcard in that will always match a string. 根据path空字符串的文档"" ,它的行为有点像一个通配符,总是匹配一个字符串。 However, path requires an exact match (the entire string be consumed by the match) but the empty string only completely consumes the empty string. 但是, path需要完全匹配(匹配消耗整个字符串),但空字符串只完全消耗空字符串。 Since pathPrefix only requires the beginning of the string to match, not the entire string be consumed, the following works: 由于pathPrefix只需要匹配字符串的开头,而不是消耗整个字符串,因此以下工作原理:

val route =
  pathPrefix("") {
    complete(
      HttpEntity(
        ContentTypes.`text/html(UTF-8)`,
        "<h1>Say hello to akka-http</h1>"))
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM