I'm trying to do some RESTFull Web service POC using Play 2.1.3
I have the following class:
case class Student(id: Long,firstName: String,lastName: String)
Now I would like to create RESTfull URI which will get Json serialised Student POJO and return the same POJO in response.
implicit val studentReads = Json.reads[Student]
implicit val studentWrites = Json.writes[Student]
def updateStudent = Action(parse.json){
request=>request.body.validate[Student].map{
case xs=>Ok(xs)}.recoverTotal{
e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
}
}
But I'm getting compilation Error -
Cannot write an instance of entities.Student to HTTP response. Try to define a
Writeable[entities.Student]
I just provided Writes[A]
as an implicit variable.
What else am I missing?
I think the problem is that the Ok() method cannot figure out the Student
needs to be transformed to json, as the arguments to Ok() may vary.
Ok(Json.toJson(xs))
Ok(xs: JsValue)
And be sure all implicits are in scope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With