I am new to Play (and somewhat new to Scala), so forgive me if this is way off the mark, though really that is why I am posting this here.
I'm trying to create a basic endpoint which goes away and does basic CRUD with mongodb.
controllers/NoteController.scala
:
package controllers
import play.api.mvc._
import play.api.libs.json._
import data.Note
class NotesController extends Controller {
def index = Action {
Note.fetch match {
case Some(notes) => Ok(Json.toJson(notes))
case None => NotFound
}
}
def show(id: Long) = Action {
val note = Note.fetch(id)
note match {
case Some(note) => Ok(Json.toJson(note))
case None => NotFound
}
}
}
data/Note.scala
package data
import com.mongodb.casbah.Imports._
object Note {
def fetch = {
Mongo.db("note").find
}
def fetch(id: Long) = {
Mongo.db("note").findOne(
MongoDBObject("id" -> id)
)
}
}
The above code doesn't actually work, but I've been banging my head against a wall for the last couple of days attempting to figure out how to get it to. I've tried using the JSON serializer that comes with casbah but had no joy there either. I just can't seem to work out how to go from the mongo data (from DBObject
) to presenting the relevant JSON.
Edit:
As mentioned in a comment to this question, the above outputs:
No Json serializer found for type Any. Try to implement an implicit Writes or Format for this type.
When it gets the the point in the code it is attempting to serialize note
as json: Ok(Json.toJson(note))