加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Spray:routing – 了解path和pathPrefix之间的区别

发布时间:2020-12-16 18:52:08 所属栏目:安全 来源:网络整理
导读:import akka.actor.Actorimport spray.routing.HttpServiceimport spray.http._import MediaTypes._import spray.json._import spray.routing.directives.CachingDirectives._import spray.httpx.encoding._trait MarginEvaluationService extends HttpServi
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有什么区别?
我不确定?运算符是否未正确包含.

解决方法

path是最终路径,而pathPrefix可以随后使用DSL与其他路径段组合.

如果你想完全匹配/你好,你应该使用路径(“你好”).

pathPrefix在类似的情况下很方便

pathPrefix("hello") {
  path("foo") {
    complete("foo")
  } ~
  path("bar") {
    complete("bar")
  }
}

这将匹配/ hello / foo和/ hello / bar.

话虽如此,我怀疑你得到的错误只是scala解析器与DSL不相处.

你可以尝试在与闭合支架相同的线上移动?吗?
我认为解析器推断出一个分号,所以它真正理解那段代码

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)
        }
      }
    }
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读