Fork me on GitHub

Scala Polyglot Tutorial

This tutorial demonstrates using Scala within WoOF.

While the tutorial is simple, it is entirely written in Scala.

Tutorial Source

Enable Scala

To enable using Scala, add the following:



The Scala to service the request is:

package net.officefloor.tutorial.scalahttpserver

import net.officefloor.web.ObjectResponse

object ScalaService {

  def service(request: ScalaRequest, response: ObjectResponse[ScalaResponse]) {
    response.send(new ScalaResponse(s"Hello ${request.message}"))

Scala Classes

As part of supporting Scala, the necessary Jackson support is included to enable sending/receiving Scala classes as JSON.

The request / response data classes are as follows:

class ScalaRequest(val message: String)
class ScalaResponse(val message: String)


WoOF is integrated with ScalaTest via traits. This allows the following ease of testing:

package net.officefloor.tutorial.scalahttpserver

import net.officefloor.scalatest.WoofRules
import org.scalatest.flatspec.AnyFlatSpec

class ScalaHttpServerTest extends AnyFlatSpec with WoofRules {

  "Call Server" should "get result" in {
    withMockWoofServer { server =>
      val request = mockRequest("/scala")
          .header("Content-Type", "application/json")
          .entity(jsonEntity(new ScalaRequest("Daniel")))
      val response = server.send(request)
      response.assertResponse(200, jsonEntity(new ScalaResponse("Hello Daniel")))



The next tutorial covers polyglot JavaScript.