I'm trying to convert a few endpoints I have to use concurrency. I have the following method for the controller:
Original method
def getHistory(id:String) = Action {
val response = historyService.getPersonHistory(id)
Ok(write(response)) as "application/json"
}
New Method
def getHistory(id:String) = Action.async {
val response = scala.concurrent.Future {
historyService.getPersonHistory(id)
}
response.map(i => Ok(i))
}
So, when we try this with a simple example process (not calling another method, but just calculating an Int) it seems to work. In the new version above, I keep getting the error:
"No implicits found for parameter writable: Writeable[historyResponse]
Cannot write an instance of models.HistoryResponse
to HTTP response. Try to define a Writeable[models.HistoryResponse]
"
I'm new to Scala, and having difficulty finding information on making writeables. What do I need to be able to return the results as before?
Thanks
You need to define an implicit val tjs: Writes[HistoryResponse]
or even better, implicit val format: Format[HistoryResponse] = Json.format[HistoryResponse]
in the companion object for HistoryResponse
, so that play can auto convert your data to json. by the way, not a good name for i
in the map function, something like "history" would be better instead of "i".
I'm not sure what you mean by "companion object" that my model itself needs to change?
@Limey You can find more here, you can also use other ways of injecting the
Writable
, but using companion object is the best practice.wrapping the result in a write (Ok(write(result)) as "application/json") solved the problem. Thanks!