Skip to content

Instantly share code, notes, and snippets.

@langley
Last active December 14, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langley/5025197 to your computer and use it in GitHub Desktop.
Save langley/5025197 to your computer and use it in GitHub Desktop.
Simple Play 2.1 Scala Json Service
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.json._
// you need this import to have combinators
import play.api.libs.functional.syntax._
// Add this to your conf/routes
// POST /JsSayHello controllers.JsonServices.sayHello
// Test it with curl
// curl --header "Content-type: application/json" --request POST --data '{"name": "Toto", "age": 32}' http://localhost:9000/JsSayHello
object JsonServices extends Controller {
// Json to Scala converter... using generic Tuple
implicit val rds = (
(JsPath \ 'name).read[String] and
(JsPath \ 'age).read[Long]
) tupled
// Json input, Json output with Error handling
// and a chance to talk about higher order functions and pattern maching
def sayHello = Action(parse.json) { request =>
request.body.validate[(String, Long)].map { // this is receiving a function literal (actually a partial function)
case (name, age) => Ok(Json.obj("status" ->"OK", "message" -> ("Hello "+name+" , you're "+age) ))
}.recoverTotal{
e => BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toFlatJson(e)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment