Warm tip: This article is reproduced from serverfault.com, please click

scala-API返回可写

(scala - API return writeable)

发布于 2020-12-11 14:14:20

我正在尝试转换一些必须使用并发的端点。我对控制器有以下方法:

原始方法

def getHistory(id:String) = Action {
  val response = historyService.getPersonHistory(id)

  Ok(write(response)) as "application/json"
}

新方法

def getHistory(id:String) = Action.async {
    val response = scala.concurrent.Future {
        historyService.getPersonHistory(id)
    }

   response.map(i => Ok(i))

}

因此,当我们通过一个简单的示例过程(而不是调用另一个方法,而只是计算一个Int)尝试这种方法时,它似乎可以工作。在上面的新版本中,我不断收到错误消息:

"No implicits found for parameter writable: Writeable[historyResponse]

无法将实例写入models.HistoryResponseHTTP响应。尝试定义一个Writeable[models.HistoryResponse]

我是Scala的新手,很难找到有关编写可写内容的信息。我需要什么才能像以前一样返回结果?

谢谢

Questioner
Limey
Viewed
11
AminMal 2020-12-11 23:34:21

你需要在的随对象中定义一个implicit val tjs: Writes[HistoryResponse],甚至更好,以便播放可以将你的数据自动转换为json。顺便说一句,不是map函数中的好名字,像“ history”这样的东西比“ i”更好。implicit val format: Format[HistoryResponse] = Json.format[HistoryResponse]HistoryResponsei