Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Demonstrates a simple Play controller with routes for listing and adding items using Scala.
// 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.addPlay Framework is a high-velocity, reactive web framework for Java and Scala, designed for building modern web applications and RESTful services. It emphasizes developer productivity, statelessness, and asynchronous I/O.
Origin & Creator
Created by Guillaume Bort and the Play team at Zenexity in 2007, later maintained by Lightbend.
Industrial Note
Play is widely used for web apps, APIs, microservices, and reactive systems where scalability, non-blocking I/O, and developer productivity are critical.