2014-06-16 30 views
1

我正在使用Play中的Scala服務充當另一服務的代理。我遇到的問題是IntelliJ給我一個類型錯誤,說我應該返回一個Future[SimpleResult]而不是Result對象。這裏是我的:類型錯誤預期:結果,實際:未來[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對象?

回答

4

由於播放WS.get()返回一個Future[Response],這你映射到Future[Result],你需要使用Action.async而不是Action.apply

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) 
    } 
} 
+0

是的...一直在與異步瞎搞{}只是試過,它的工作表示感謝...認爲值得把問題留下來嗎? –

相關問題