Scalatra is a Scala web framework that follows the principles of the Sinatra Ruby web framework. It was originally known as Step and it is the framework behind the RESTful backend that is used by LinkedIn Signal.

In Scalatra , as in all Sinatra-like web frameworks, the developer defines matching routes and the code that handles them:

package org.scalatra class ScalatraExample extends ScalatraServlet { // send a text/html content type back each time before { contentType = "text/html" } // parse matching requests, saving things prefixed with ':' as params get("/date/:year/:month/:day") { <ul> <li>Year: {params("year")}</li> <li>Month: {params("month")}</li> <li>Day: {params("day")}</li> </ul> } // produce a simple HTML form get("/form") { <form action='/post' method='POST'> Post something: <input name='submission' type='text'/> <input type='submit'/> </form> } // handle POSTs from the form generated above post("/post") { <h1>You posted: {params("submission")}</h1> } // respond to '/' with a greeting get("/") { <h1>Hello world!</h1> } // send redirect headers get("/see_ya") { redirect("http://google.com") } // set a session var get("/set/:session_val") { session("val") = params("session_val") <h1>Session var set</h1> } // see session var get("/see") { session("val") match { case Some(v:String) => v case _ => "No session var set" } } // Actions that return byte arrays render a binary response get("/report.pdf") { contentType = "application/pdf" val pdf = generatePdf() pdf.toBytes } notFound { response.setStatus(404) "Not found" } }

InfoQ had a Q&A with co-author Ross Baker, about the Scalatra project:

InfoQ: Sinatra is a popular Ruby framework that is being ported into several languages? What do you think are its best features and what attracted you to it?

Ross: The Sinatra family of frameworks is compelling because they are minimal: if you already know the target language and the basics of HTTP, then you can be almost instantly productive with these frameworks.

InfoQ: What was your reason for choosing Scala for a web framework?

Ross: I spent four years in school learning the elegance of functional programming. I invested the next decade in Java, appreciating its vast array of libraries, if not the language itself. Along came Scala, which neatly unifies these two worlds. Scala was a no-brainer for me.

InfoQ: What were the features that you were missing from other Scala frameworks, e.g. Lift?

Ross: Lift is amazing, both as a framework and a community, but I struggle with some of its core assumptions. Specifically, Lift embraces session state and hides the HTTP, whereas I tend to embrace HTTP and avoid session state. Now, it's not black and white: you can build a RESTful app with Lift or you can build a stateful app with Scalatra, but each framework is tuned for a different type of application. I'm glad that we have both.

InfoQ: Would you like to give us an overview of the various parts that make up a Scalatra application?

Ross: Scalatra is a simple DSL. You can write your entire application in a single class. (Whether this is prudent or not of course depends on the scope of your application.) That, plus a few trivial lines of web.xml, are sufficient for a Scalatra application

InfoQ: What would be your advice to teams that are considering switching from a Java web framework to Scalatra? What are the common pitfalls they should be careful of and what would be a good migration strategy?

Ross: To migrate a Java application, I recommend declaring a ScalatraFilter within the same web-app. You can then migrate your pages one at a time. If Scalatra finds a matching route, then it will handle it. Otherwise, the request will pass through to the legacy servlet. Also, keep in mind that the interoperability between Java and Scala is bidirectional. This means that there is no strict need to port to Scala layer by layer. Port just the units of code that require an update, and don't be afraid to leave working, time-tested Java code as Java until your requirements change again.

InfoQ: Recently LinkedIn announced that they've used Scalatra for LinkedIn Signal. Do you have any other examples of deployments? For what types of applications do you see people using Scalatra?

Ross: In addition to LinkedIn, ChaCha is successfully using Scalatra today for internal APIs and admin apps. They particularly like the Scalate integration for templating. I am aware of at least two major Scalatra projects in progress at startups, which we will be excited to announce in the future. RESTful APIs are very common as Scalatra apps. Just as Scala is a scalable language, Scalatra is a scalable framework. You can quickly prototype your application with inline HTML and business logic. As your project matures, you can lean on the compiler and test framework as you refactor into an n-tier, enterprise-grade application. Or not. We leave the architecture up to you.

InfoQ: With M1 made available in August when are you planning for the final release? What is the roadmap of your project and what features would you like to see in the next releases?

Ross: Some near-term initiatives include comet support, separating the DSL from the Servlet/Filter for easier reloading with JRebel, and launching a proper website: we're going to eat our own dogfood. There is also interest in OSGi support, nested routers, and enhanced routing matching options. Expect another milestone with the release of Scalate 1.3, and a thorough review of any inconsistencies in the API before we bless it with the 2.0.0-final tag.

Scalatra is free to use and there are various option for downloading it.

You can find more information on Scala and Web Frameworks, right here on InfoQ!