2016-07-14 50 views
1

我正在使用Http Akka和Scala。 我有這樣的路線:正則表達式的http Akka路由不在編譯

object Route { 
    val route = 

    path("items"/"card"/"""\w+""".r) { 
     get { 
     complete { 
      EntitiesData.someEntity 
     } 
     } 
    } 
} 

出於某種原因,它不與此錯誤編譯:

Error:(18, 11) type mismatch; 
found : akka.http.scaladsl.server.Route 
    (which expands to) akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] 
required: String => (akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]) 
     get { 

當我刪除了正則表達式的一部分,或者將其更改爲常規字符串的東西似乎工作精細。

回答

3

您需要修改你的代碼:

object Route { 
    val route = 

    path("items"/"card"/"""\w+""".r) { matched => 
     get { 
     complete { 
      EntitiesData.someEntity 
     } 
     } 
    } 
} 

從阿卡的PathMatcher DSL文檔:

You can use a Regex instance as a PathMatcher1[String], which matches whatever the regex matches and extracts one String value. A PathMatcher created from a regular expression extracts either the complete match (if the regex doesn't contain a capture group) or the capture group (if the regex contains exactly one capture group). If the regex contains more than one capture group an IllegalArgumentException will be thrown.

所以,當你在一個路徑指示使用正則表達式,它會沿着通匹配到該指令下的部分。這就是你的代碼所缺少的。