我正在嘗試將Play Framework從2.1升級到2.2,並且遇到新的Action.asyc語法遇到困難。例題遷移到2.2,正確使用Action.asyc
我得到:
->Action.async {
type mismatch; found : play.api.mvc.Action[play.api.mvc.AnyContent]
required: scala.concurrent.Future[play.api.mvc.SimpleResult]
經過大量谷歌的,我切換到SO社區。示例代碼通道(大量剝離出來無關緊要的細節)這裏:
播放2.1
def homeContent(origin: String, segment: String) = Secured {
Action {
implicit request =>
val maybeOrigin = Origin.allOrigins.find(p => p.slug == origin)
maybeOrigin match {
case None => NotFound
case Some(originPlace) => {
val futureResult = scala.concurrent.Future {
DealCard.getTopDealCardsFor(origin, segment)
}
Async {
futureResult.map {
case (topDeals) =>
Ok(「」).as("application/json")
}
}
}
}
}
}
播放2.2.2
def homeContent(origin: String, segment: String) = Secured {
Action.async {
implicit request =>
val maybeOrigin = Origin.allOrigins.find(p => p.slug == origin)
maybeOrigin match {
case None => Future.successful(NotFound)
case Some(originPlace) => {
Action.async {
val futureResult = scala.concurrent.Future {
DealCard.getTopDealCardsFor(origin, segment)
}
futureResult.map {
case (topDeals) =>
Ok("").as("application/json")
}
}
}
}
}
的問題是,第二Action.async是返回a:play.api.mvc.Action [play.api.mvc.AnyContent]而不是scala.concurrent.Future [play.api.mvc.SimpleResult]。令人困惑的是Ok()應該是Future [play.api.mvc.SimpleResult]。如果我將它包裝在Future.successful中,它會抱怨Async((scala.concurrent.Future [scala.concurrent.Future [play.api.mvc.SimpleResult]]))的超載。 – andrewl
編輯了這個問題,以澄清我遇到麻煩的地方。感謝你的幫助。 – andrewl
我認爲你誤解了Action.async如何在2.2中工作。它不像舊的'Async'塊,你只是圍繞着'Future',它取代了整個操作。它們不應該嵌套。 – Ryan