Play2,scala:如何使用另一个控制器的控制器结果?

时间:2015-04-08 16:24:51

标签: scala playframework playframework-2.3

我有一个应用程序,它使用了游戏2.3.8,scala和(如果有关系)play-auth。

有一个控制器,方法:

def foo(id: Long) = StackAction(AuthorityKey -> Everybody) { implicit request =>
  //code forming json   
  Ok(json) 
}

如何从另一个控制器获取json? 我尝试了一些东西,但没有成功:

def bar(id : Long) = StackAction(AuthorityKey -> Everybody){ implicit request =>
  val futureResponse = AnotherController.foo(id).apply(request)
  val result = Await.result(futureResponse, Timeout(5, TimeUnit.SECONDS).duration)
  Logger.debug("_______________________" + result.body) //dont't know how to convert that to json
  //handle json there
  Ok(newResult)
}

如何做到这一点?

1 个答案:

答案 0 :(得分:0)

试试这个

 def bar(id : Long) = StackAction(AuthorityKey -> Everybody){ implicit request =>

      val futureResponse: Future[JsValue] = 
          AnotherController.foo(id).apply(request).flatMap{ res =>
              res.body |>>> Iteratee.consume[Array[Byte]]()
          }.map(bytes => Json.parse(new String(bytes,"utf-8")))

      val json = Await.result(futureResponse, Timeout(5, TimeUnit.SECONDS).duration)
      Logger.debug("_______________________" + json) //dont't know how to convert that to json
      //handle json there
      Ok(json)
    }