简体   繁体   English

Akka http 匹配路径

[英]Akka http match path

I have a case where I want to validate something only for a particular route ie end with "_xxx" .我有一个案例,我只想验证特定路线的某些内容,即以 "_xxx" 结尾。 But if I do the way its shown below then pathSuffix extract the path and leaves a suffix unmatched suffix in the route object and hence can not be used by caller of this function as the route object is changed.但是,如果我按照如下所示的方式进行,那么 pathSuffix 会提取路径并在路由对象中留下一个不匹配的后缀,因此当路由对象更改时,此函数的调用者无法使用该后缀。

def apply(route: Route): Route =
    pathSuffix("_xxx") {
      extractRequest { request =>
          entity(as[SomeObject]) { content =>
            if (content.records.size > limit)
              complete(
                StatusCodes.BadRequest -> "msg"
              )
            else
              route // talking about this.
          }
      }
    }

Can I have a way to execute this piece of code for only those uri path that ends with "_xxx" and if the condition does not satisfy then return original route so it can be used further !!我可以有办法只为那些以“_xxx”结尾的uri路径执行这段代码,如果条件不满足则返回原始路径以便进一步使用!!

you can apply a pattern matching as:您可以将模式匹配应用为:

object routeSuffix {
   def unapply(arg: String): Boolean = arg.endsWith("_xxx")
}

if (content.records.size > limit)
   complete(
      StatusCodes.BadRequest -> "msg"
   )
else {
  route match {
    case routeSuffix() => // what you want to do 
    case _ => // default case, do nothing or anything else
  }
}
def apply(route: Route): Route =
    extractRequest {
      case HttpRequest(POST, Uri.Path("dsd/_xxx"), _, _, _) =>
        entity(as[SomeObject]) { content =>
          if (content.records.size > limit)
            complete(
              StatusCodes.BadRequest -> "msg"
            )
          else
            route
        }
      case _ => route
    }

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

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