Learn PLAY with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Play Simple REST API
// app/controllers/ItemController.scala
package controllers
import play.api.mvc._
import play.api.libs.json._
import javax.inject._
case class Item(id: Int, name: String)
object Item { implicit val format = Json.format[Item] }
@Singleton
class ItemController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
private var items = Seq(Item(1, "Item 1"), Item(2, "Item 2"))
def list = Action {
Ok(Json.toJson(items))
}
def add = Action(parse.json) { request =>
val newItem = request.body.as[Item]
items = items :+ newItem
Created(Json.toJson(newItem))
}
}
// conf/routes
GET /items controllers.ItemController.list
POST /items controllers.ItemController.add
Demonstrates a simple Play controller with routes for listing and adding items using Scala.