long
和extractStrictEntity
之间有什么区别?
这是toStrictEntity
路线代码:
extractStrictEntity
这是import scala.concurrent.duration._
val route = extractStrictEntity(3.seconds) { entity =>
complete(entity.data.utf8String)
}
路线代码:
toStrictEntity
答案 0 :(得分:0)
你的第二条路线,即toStrictEntity
的路线,不会编译;它失败了:
value data is not a member of akka.http.scaladsl.server.RequestContext
至于两个指令之间的区别,extractStrictEntity
调用toStrictEntity
:
def extractStrictEntity(timeout: FiniteDuration): Directive1[HttpEntity.Strict] =
toStrictEntity(timeout) & extract(_.request.entity.asInstanceOf[HttpEntity.Strict])
// ...
def toStrictEntity(timeout: FiniteDuration): Directive0 =
// ...
以下两条路线基本相同:
val route1 = extractStrictEntity(3.seconds) { entity =>
complete(entity.data.utf8String)
}
val route2 = toStrictEntity(3.seconds) {
extractRequest { req =>
req.entity match {
case strict: HttpEntity.Strict =>
complete(strict.data.utf8String)
case _ =>
complete("Request entity is not strict!")
}
}
}
extractStrictEntity
会返回Directive1[HttpEntity.Strict]
,这意味着它会提取一个HttpEntity.Strict
值。另一方面,toStrictEntity
返回Directive0
,这意味着它不会提取任何内容 - 因此在上面的示例中嵌套了对extractRequest
的调用。