Scala Lesson 50 – Web Frameworks | Dataplexa

Web Frameworks in Scala

In this lesson, you will learn about web frameworks in Scala and how Scala is used to build modern web applications and REST APIs. Scala web frameworks combine the power of the JVM with functional and object-oriented programming styles.

Scala is widely used in enterprise backends, microservices, and high-performance web systems.


Why Use Scala for Web Development?

Scala offers several advantages when building web applications:

  • Runs on the JVM with access to Java libraries
  • Strong type safety reduces runtime errors
  • Functional programming improves reliability
  • Excellent performance and scalability

Many large-scale systems choose Scala for backend services that require robustness and maintainability.


Popular Scala Web Frameworks

Scala has several mature and production-ready web frameworks.

  • Play Framework
  • Akka HTTP
  • Http4s
  • Finatra

Each framework has a different philosophy and use case.


Play Framework Overview

The Play Framework is the most popular Scala web framework. It follows a reactive and stateless architecture and is designed for building REST APIs and web applications.

Play uses asynchronous, non-blocking I/O by default.


Creating a Play Framework Project

You can create a Play project using SBT:

sbt new playframework/play-scala-seed.g8

This command generates a ready-to-run Scala Play application.


Understanding Play Controllers

Controllers handle incoming HTTP requests and return responses. Below is a simple controller example.

package controllers

import javax.inject._
import play.api.mvc._

@Singleton
class HomeController @Inject()(cc: ControllerComponents)
  extends AbstractController(cc) {

  def index() = Action {
    Ok("Welcome to Scala Play!")
  }
}

This controller responds with a simple text message.


Routing in Play Framework

Routes map URLs to controller actions. Routes are defined in the conf/routes file.

GET   /   controllers.HomeController.index

This route maps the root URL to the index method.


Returning JSON Responses

Scala web applications often return JSON data. Play provides built-in JSON support.

import play.api.libs.json._

def jsonResponse() = Action {
  Ok(Json.obj(
    "name" -> "Scala",
    "type" -> "Web Framework"
  ))
}

This endpoint returns structured JSON output.


Akka HTTP Overview

Akka HTTP is a low-level, powerful framework built on Akka. It is ideal for microservices and high-performance APIs.

Akka HTTP emphasizes:

  • Asynchronous programming
  • Actor-based concurrency
  • Explicit request handling

Basic Akka HTTP Example

Below is a minimal Akka HTTP server example.

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._

implicit val system = ActorSystem("my-system")

val route =
  path("hello") {
    get {
      complete("Hello from Akka HTTP!")
    }
  }

Http().newServerAt("localhost", 8080).bind(route)

This creates a simple HTTP endpoint at /hello.


Http4s Overview

Http4s is a purely functional web framework built on top of Cats Effect.

It focuses on:

  • Functional purity
  • Immutability
  • Type safety

Choosing the Right Framework

Different frameworks suit different needs:

  • Play → Full-stack web applications
  • Akka HTTP → Microservices and concurrency
  • Http4s → Functional programming enthusiasts

Your choice depends on project size, performance needs, and architectural style.


📝 Practice Exercises


Exercise 1

Create a simple Play controller that returns a text response.

Exercise 2

Define a route that maps a URL to a controller method.

Exercise 3

Create a basic Akka HTTP route that returns JSON.


✅ Practice Answers


Answer 1

def hello() = Action {
  Ok("Hello Scala")
}

Answer 2

GET /hello controllers.HomeController.hello

Answer 3

complete("""{"status":"ok"}""")

What’s Next?

In the next lesson, you will build a CLI Application in Scala, applying what you’ve learned to a practical project.