Play for Scala中的转发返回空响应

时间:2016-07-11 10:30:33

标签: scala playframework playframework-2.0

下面的HTTP转发(futureResponse函数)会向浏览器返回一个空响应。任何想法有什么不对和/或我应该在哪里看?

  val request: WSRequest = ws.url("http://somehost/url2")
  val request2: WSRequest = request.withHeaders("Accept" -> "application/json")
  val data = Json.obj(
        "aaa" -> some_data1,
        "bbb" -> some_data2
   )
   val futureResponse: Future[JsValue] = request2.post(data).map {
     response => 
         val json= Json.obj(
             "ccc" -> "111",
             "ddd" -> "222"
            )
         json
   }

   Ok(json)

当我在浏览器中收到消息时,响应中的数据为空:

enter image description here

1 个答案:

答案 0 :(得分:1)

使操作异步并回复Future [Result],如:

def someAction = Action.async { implicit request =>
  val request: WSRequest = ws.url("http://somehost/url2")
  val request2: WSRequest = request.withHeaders("Accept" -> "application/json")
  val data = Json.obj(
    "aaa" -> some_data1,
    "bbb" -> some_data2
  )
  val futureResponse: Future[JsValue] = request2.post(data).map { response => 
     val json= Json.obj(
         "ccc" -> "111",
         "ddd" -> "222"
     )

     Ok(json)
  }

  futureResponse
}