当前位置: 动力学知识库 > 问答 > 编程问答 >

scala - How to match all paths in Akka HTTP

问题描述:

Most Akka HTTP examples show it is really easy to define Routes using path.

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". The empty string, "", only matches the root path and "*" matches the literal path "/*". The parameter to path is required and cannot simply be omitted.

网友答案:

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 thing to that the Route can be any Directive, not only path directives. Thus, the above example could achieve the desired results by just by dropping the path completely:

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

Use a different PathMatcher with path

path does not actually take a String as a parameter. path("hello") is actually pathPrefix(_segmentStringToPathMatcher("hello")) after implicit conversion.

You can use Remaining which matches everything left.

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.

Use pathPrefix("") instead of path("")

According to the documentation for path empty string, "", does in behave a little like a wildcard in that will always match a string. However, path requires an exact match (the entire string be consumed by the match) but the empty string only completely consumes the empty string. Since pathPrefix only requires the beginning of the string to match, not the entire string be consumed the following works:

val route =
  pathPrefix("") {
    complete(
      HttpEntity(
        ContentTypes.`text/html(UTF-8)`,
        "<h1>Say hello to akka-http</h1>"))
  }
分享给朋友:
您可能感兴趣的文章:
随机阅读: