1. Home
  2. /
  3. Play
  4. /
  5. Simple REST API

Simple REST API - Play Typing CST Test

Loading…

Simple REST API — Play Code

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.add

Play Language Guide

Play 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.

Primary Use Cases

  • ▸Building reactive web applications
  • ▸Creating RESTful APIs
  • ▸Developing microservices and backend services
  • ▸Rapid prototyping with hot reload
  • ▸High-concurrency applications

Notable Features

  • ▸Type-safe routing and templates
  • ▸Asynchronous, non-blocking I/O
  • ▸Built-in support for JSON and REST
  • ▸Hot-reload for development
  • ▸Integrated testing framework

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.

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher