play

Play Framework

Play Framework patterns for building type-safe web applications and REST APIs in Scala

Details

Language / Topic
scalaScala
Category
framework
Compatible Frameworks
play

Rules

balanced
- Define routes in `conf/routes` with `GET /users/:id controllers.UserController.show(id: Long)` — path and query params are typed.
- Use `Action.async { implicit request => Future[Result] }` for non-blocking handlers — never block with `Await.result` in Action bodies.
- Map domain types to/from JSON with `Json.format[T]` macro or explicit `Reads[T]`/`Writes[T]` — always validate input with `JsValue.validate[T]`.
- Inject dependencies via constructor: `class UserController @Inject()(cc: ControllerComponents, svc: UserService)` — bind in a `Module extends AbstractModule`.
- Return typed results: `Ok(json)`, `Created(body)`, `BadRequest(errors)`, `NotFound` — always set the content type header.
- Use `wsClient.url("...").withRequestTimeout(5.seconds).get()` for outbound HTTP — inject `WSClient`, never instantiate it directly.
- Read typed config with `configuration.get[String]("app.secret")` or bind case class config via `@ConfiguredBinding`.
- Use `EssentialFilter` for cross-cutting concerns (auth, logging) — add to `HttpFilters` via Guice binding.