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.addPlay 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.
Quick Explain
- ▸Play follows MVC architecture for structured and maintainable code.
- ▸Supports both Java and Scala, offering type-safe routing and templates.
- ▸Built-in asynchronous processing for high-performance, non-blocking applications.
- ▸Provides integrated testing, asset management, and RESTful API support.
- ▸Hot-reloading for rapid development without server restarts.
Core Features
- ▸MVC architecture
- ▸Controllers, models, and views
- ▸Asynchronous programming support
- ▸Template engines (Twirl for Scala/Java)
- ▸Dependency injection support
Learning Path
- ▸Week 1: Java/Scala basics and MVC concepts
- ▸Week 2: Routing, controllers, and views
- ▸Week 3: Asynchronous programming and Futures
- ▸Week 4: REST APIs and JSON handling
- ▸Week 5: Testing, WebSockets, and deployment
Practical Examples
- ▸Building a blog with REST API and web frontend
- ▸Developing a backend API for a mobile app
- ▸Implementing real-time chat using WebSockets
- ▸Creating dashboards with dynamic data
- ▸High-concurrency applications with non-blocking database access
Comparisons
- ▸Play vs Spring Boot -> Play is reactive and stateless; Spring Boot is larger but more feature-rich
- ▸Play vs Django -> Play is JVM-based; Django is Python-based with similar MVC pattern
- ▸Play vs Express.js -> Play offers type-safe routing and JVM performance; Express.js is lightweight JS runtime
- ▸Play vs Laravel -> Play is reactive and Java/Scala-based; Laravel is PHP-based with expressive syntax
- ▸Play vs Rails -> Play is asynchronous and reactive; Rails emphasizes convention over configuration
Strengths
- ▸High-performance and scalable
- ▸Reactive programming support
- ▸Hot reload for faster development
- ▸Supports both Java and Scala
- ▸Strong community and Lightbend ecosystem integration
Limitations
- ▸Smaller ecosystem than Spring Boot or Laravel
- ▸Learning curve for reactive and asynchronous programming
- ▸Less suitable for small static websites
- ▸Requires JVM knowledge
- ▸May be overkill for simple CRUD apps
When NOT to Use
- ▸Small static websites
- ▸Projects requiring minimal server setup
- ▸When JVM installation is not feasible
- ▸Simple CRUD apps with no asynchronous requirements
- ▸Teams unfamiliar with Java or Scala
Cheat Sheet
- ▸sbt run -> start development server
- ▸sbt compile -> compile project
- ▸sbt test -> run tests
- ▸conf/routes -> define application routes
- ▸app/controllers/ -> define controllers
FAQ
- ▸Is Play free? -> Yes, open-source under Apache 2.0 License
- ▸Does Play require JVM? -> Yes, Java or Scala runtime required
- ▸Can Play be used for REST APIs? -> Yes, with built-in JSON and routing support
- ▸Does Play support WebSockets? -> Yes, natively via Akka Streams
- ▸Is Play suitable for enterprise apps? -> Yes, especially reactive and high-concurrency apps
30-Day Skill Plan
- ▸Master asynchronous actions and Futures
- ▸Use Twirl templates effectively
- ▸Implement authentication and authorization
- ▸Optimize performance and non-blocking I/O
- ▸Write tests and integrate CI/CD pipelines
Final Summary
- ▸Play Framework is a reactive, high-performance web framework for Java and Scala.
- ▸Follows MVC with asynchronous, non-blocking I/O.
- ▸Supports Twirl templates, REST APIs, WebSockets, and testing.
- ▸Offers hot reload for rapid development.
- ▸Extensible and scalable for enterprise-grade applications.
Project Structure
- ▸app/ - controllers, models, views
- ▸conf/ - routes, application.conf, logging
- ▸public/ - static assets (JS, CSS, images)
- ▸test/ - unit and functional tests
- ▸build.sbt - project build configuration
Monetization
- ▸Develop SaaS platforms
- ▸Build enterprise web and mobile backends
- ▸Offer consulting for Play/Scala/Java
- ▸Create premium microservices or modules
- ▸Training and workshops for Play Framework developers
Productivity Tips
- ▸Use hot reload to speed up development
- ▸Leverage reactive streams and async actions
- ▸Reuse Twirl templates and components
- ▸Cache frequent data and views
- ▸Automate testing and CI/CD pipelines
Basic Concepts
- ▸Routes - map URLs to controller actions
- ▸Controllers - handle request logic
- ▸Models - represent business domain and data
- ▸Views - Twirl templates for HTML rendering
- ▸Asynchronous actions - non-blocking request handling
Official Docs
- ▸https://www.playframework.com/documentation/latest
- ▸Play GitHub Repository
- ▸Lightbend Play Tutorials and Guides