Simple HTTP Server With Scalatra In 20 Lines Of Code

In this tutorial I will show how easy it is to create a HTTP server in Scala using Scalatra — a free web micro-framework that such giants as as BBC, the Guardian newspaper, and Netflix enjoy to use in production (if we believe Scalatra website). We are going to build a simple REST API that we return "Scalatra rules!" on GET request.

For those who prefer watching to reading, I have also created a video tutorial.

Let's create a new SBT project and add two dependencies to our build.sbt file. First, Scalatra itself, and, second, Jetty — a Web server and javax servlet container that we will need to run Scalatra on.

In the end, your build.sbt file should look like this:

name := "scalatra"

version := "0.1"

scalaVersion := "2.12.8"

libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % "2.5.4",
"org.eclipse.jetty" % "jetty-webapp" % "9.4.12.v20180830"
)

The next step is to create and configure a web server to be able to run Scalatra on it. For this purpose, I create a Scala object WebServiceBuilder that holds a function that does it for us:

import javax.servlet.Servlet
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext

object WebServiceBuilder {
def buildWebService(port: Integer, webServiceClass: Class[_ <: Servlet]): Server = {
val server = new Server(port)
val context = new WebAppContext()
context.setContextPath("/")
context.setResourceBase("/tmp")
context.addServlet(webServiceClass, "/*")
server.setHandler(context)
server
}
}

Let's stop here for a minute and have a closer look on the configuration of the context.

In this line we set the context path. It is just a prefix for a URL which is used to select a context for our incoming request.

context.setContextPath("/")

Typically a URL in a Java servlet server has the following format http://hostname.com/contextPath/servletPath/pathInfo Each of the path elements can have zero or more / separated elements. If there is no context path, the context is referred to as the root context. The root context must be configured as /. How you set the context path depends on how you deploy the web application. We do not need a context path in our app, so we just refer to the root with/ in our configuration of the context.

In

context.setResourceBase("/tmp")

we define a path to the folder with our resources. It is not relevant for this tutorial, however, to make the server work correctly, we still need to pass this configuration to the context.

In the next line

context.addServlet(webServiceClass, "/*")

we pass the class of our webservice (which is a servlet) and the path to map servlet to.

When the context is configured, we pass it to the server:

server.setHandler(context)

Voalia! The configuration of the server is done and we can create our REST API.

For this purpose, I created a new Scala class WebService where I defined my REST API:

import org.scalatra.ScalatraServlet

class WebService extends ScalatraServlet {
get("/") {
"Scalatra rules!"
}
}

In the Main, I instantiated a server instance and made it to start on the port 8080.

import org.eclipse.jetty.server.Server
object Main extends App {

val server: Server = WebServiceBuilder.buildWebService(8080, classOf[WebService])
server.start()
}

That's it! We have a running server that could proceed our HTTP requests!

Go to http://localhost:8080/ and enjoy the results:

P.S. For those who want to have a look at the source code, could find it on my Bitbucket.