TypeError预期:结果,实际:未来[SimpleResult]

时间:2014-06-16 19:31:00

标签: scala playframework

我正在使用Play中的Scala服务充当其他服务的代理。我遇到的问题是IntelliJ给我一个类型错误,说我应该返回Future[SimpleResult]而不是结果对象。这就是我所拥有的:

def getProxy(proxyUrl: String) = Action { request =>
  val urlSplit = proxyUrl.split('/')
  urlSplit(0) 
  WS.url(Play.current.configuration.getString("Services.url.um") + proxyUrl).get().map { response =>
    val contentType = response.header("Content-Type").getOrElse("text/json")
    Ok(response.body)
  }
}

如何解决此问题,以便返回Result对象?

1 个答案:

答案 0 :(得分:4)

由于Play WS.get()返回Future[Response],您要映射到Future[Result],因此需要使用Action.async代替Action.apply:< / p>

def getProxy(proxyUrl: String) = Action.async { request =>
    val urlSplit = proxyUrl.split('/')
    urlSplit(0) 
    WS.url(Play.current.configuration.getString("Services.url.um") + proxyUrl).get().map { response =>
        val contentType = response.header("Content-Type").getOrElse("text/json")
        Ok(response.body)
    }
}