Building RESTful APIs

Play makes it easy to build RESTful endpoints using routes and JSON support.

Define a Route

GET   /users/:id     controllers.UserController.getUser(id: Int)

Example JSON Response

def getUser(id: Int) = Action {
  val userJson = Json.obj("id" -> id, "name" -> s"User$id")
  Ok(userJson)
}

Enable JSON

import play.api.libs.json._
case class User(id: Int, name: String)
implicit val userFormat = Json.format[User]
← PrevNext →