This tutorial demonstrates using Scala within WoOF.
While the tutorial is simple, it is entirely written in Scala.
To enable using Scala, add the following:
<dependency> <groupId>net.officefloor.scala</groupId> <artifactId>officescala</artifactId> </dependency> <dependency> <groupId>net.officefloor.scala</groupId> <artifactId>officescalatest</artifactId> <scope>test</scope> </dependency>
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}")) } }
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:
@HttpObject 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") .method(httpMethod("POST")) .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.