代码之家  ›  专栏  ›  技术社区  ›  Avba

最后一个可选元素的akka路径匹配

  •  0
  • Avba  · 技术社区  · 6 年前

    pathPrefix("the-endpoint" / Segment / Segment.?) { (left[String], right: Option[String]) => ... }
    

    问题是,只有在添加尾部“/”斜杠字符时才会调用此路径:

    i、 e.不要添加最后一个路径部分:

    curl localhost:12345/the-endpoint/firstsegment
    The requested resource could not be found
    

    但是

    curl localhost:12345/the-endpoint/firstsegment/
    ... all good , gets to the path as expected ...
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   Jeffrey Chung    6 年前

    你可以用这个 ignoreTrailingSlash 指令:

    ignoreTrailingSlash {
      path("the-endpoint" / Segment / Segment.?) { (left: String, right: Option[String]) =>
        // ...
      }
    }