所以我有以下行动,我想找到一种直接返回响应的方法,而不必将其映射到每个可能的状态代码的结果,即跳过if-else部分。
def testAction = Action { implicit requestIn => {
val requestOut : WSRequest = WS.url("test-domain-name:9998")
val queryString = requestIn.queryString.map { case (k,v) => k -> v.mkString }
val futureResponse : Future[WSResponse] = requestOut.withQueryString(queryString.toList: _*).get()
val response = Await.result(requestOut.withQueryString(queryString.toList: _*).get(), 5 seconds)
if(response.status == 200) {
Ok(response.xml)
} else {
BadRequest(response.body)
}
}
答案 0 :(得分:5)
你不应该等待结果。 Play框架支持异步操作:
def testAction = Action.async { implicit requestIn =>
val requestOut: WSRequest = WS.url("test-domain-name:9998")
val queryString = requestIn.queryString.map { case (k, v) => k -> v.mkString }
val futureResponse: Future[WSResponse] = requestOut.withQueryString(queryString.toList: _*).get()
futureResponse
}
隐式转换:
implicit def Response2Result(response: Future[WSResponse]): Future[Result] = {
response map {
response =>
val headers = response.allHeaders map {
h => (h._1, h._2.head)
}
Result(ResponseHeader(response.status, headers), Enumerator(response.body.getBytes))
}
}
请参阅github issue。
答案 1 :(得分:3)
Mon Calamari的答案变化,Play 2.5
implicit def Response2Result(response: Future[WSResponse]): Future[Result] = {
response map {
response =>
val headers = response.allHeaders map {
h => (h._1, h._2.head)
}
Result(ResponseHeader(response.status, headers), Strict(response.bodyAsBytes, None))
}
}
答案 2 :(得分:0)
这是基于Barak BN和Mon Calamari的回答的更新答案,适用于Play 2.6:
import play.api.http.HttpEntity
import play.api.libs.ws.WSResponse
import scala.concurrent.Future
def response2Result(response: Future[WSResponse]): Future[Result] = {
response map {
response =>
val headers = response.headers
.map { h => (h._1, h._2.head) }
.filter { _._1.toLowerCase != "content-length" }
Result(
ResponseHeader(response.status, headers),
HttpEntity.Strict(response.bodyAsBytes, Some(response.contentType))
)
}
}
主要区别是在2.6中用allHeaders
替换headers
并删除"内容长度"来自结果的标头因为" 不允许显式Content-Length
标头"由Akka。