Akka HTTP中的extractStrictEntity和toStrictEntity有什么区别?

时间:2018-04-24 15:16:23

标签: scala akka-http

longextractStrictEntity之间有什么区别? 这是toStrictEntity路线代码:

extractStrictEntity

这是import scala.concurrent.duration._ val route = extractStrictEntity(3.seconds) { entity => complete(entity.data.utf8String) } 路线代码:

toStrictEntity

1 个答案:

答案 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的调用。