简体   繁体   中英

Create Writeable to HTTP response in play framework

I defined a class in models/ResponseQuery as follows:

case class ResponseQuery(
  query: String,
  tableArray: Array[String]
)

In a file in app/controllers/ I return an object of type responseQuery

var responseQuery: models.ResponseQuery
Future.successful(Ok(responseQuery))

However, I get the following error while compiling:

Cannot write an instance of models.ResponseQuery to HTTP response . Try to define a Writeable[models.ResponseQuery]

I appreciate any help in solving this error.

Try with companion object to ResponseQuery and put implicit format for it.

import play.api.libs.json.Json

object ResponseQuery {
  implicit format = Json.format[ResponseQuery]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM