Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2 - Can't return Json object in Response

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?

like image 657
danny.lesnik Avatar asked Aug 23 '13 23:08

danny.lesnik


1 Answers

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.

  1. You may return an Ok(Json.toJson(xs))
  2. You may explicitly point the desired type: Ok(xs: JsValue)

And be sure all implicits are in scope

like image 143
serejja Avatar answered Oct 16 '22 06:10

serejja