scala - 喷:路由——了解path和pathPrefix的区别

标签 scala spray

import akka.actor.Actor
import spray.routing.HttpService
import spray.http._
import MediaTypes._
import spray.json._
import spray.routing.directives.CachingDirectives._
import spray.httpx.encoding._

trait MarginEvaluationService extends HttpService {
  import ClassSerializer._
  import spray.httpx.SprayJsonSupport._
  val myRoute = {

      pathPrefix("hello") {
        get {
          respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
            complete {
              <html>
                <body>
                  <h1>Say hello to <i>spray-routing</i> on <i>spray-can</i>!</h1>
                </body>
              </html>
            }
          }
        }
      }
      ~
      pathPrefix("testjson") {
        get {
          entity(as[TestC]) { c =>
            respondWithMediaType(`application/json`) {
              complete(c)
            }
          }
        }
      }
   }
}

route被窃听:

Error:(49, 1) illegal start of simple expression pathPrefix("testjson") { ^


path有什么区别和 pathPrefix ?
我不确定 ~运算符未正确包括在内。

最佳答案

docs :

路径(x) : 等价于 rawPathPrefix(slash().concat(segment(x)).concat(pathEnd()))。它匹配前导斜杠,后跟 x 和结尾。

路径前缀(x) : 等价于 rawPathPrefix(slash().concat(segment(x)))。它匹配前导斜杠后跟 x,然后留下不匹配的后缀。

关于scala - 喷:路由——了解path和pathPrefix的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31858814/

相关文章:

scala - 无法在 Scala 案例类中定义名为 `wait` 的属性

scala - 有没有办法在 Scala 中模拟 Singleton 对象

scala - 为什么在访问简单的喷涂路线时会得到 "The requested resource could not be found."?

json - 无法使用spray-json解码json HttpEntity

scala - 这可以做成尾递归吗?

scala - 将Scala映射转换为列表

javascript - Highcharts,导出时如何本地化语言

scala - 如果 A 和 B 是单子(monad),如何将 A[B[C]] 转换为 B[A[C]]?

scala - Spray、Akka-http 和 Play,这是新 HTTP/REST 项目的最佳选择

spray - 如何在 SprayTest 中模拟带有 json 正文的 POST 请求?