4 Sinatra inspired frameworks on the JVM that will boost your productivity

A lot of exciting things have happened on the JVM in the last couple of years. We've seen several interesting languages and frameworks come to life that wants to keep moving the great JVM environment forward.

One type of framework where we've seen this activity, has been the web application frameworks. In particular Sinatra inspired frameworks made to increase your productivity.

Let's take a look at a few of these Sinatra-like frameworks.


Many argues that the Java environment is polluted by heavy frameworks and bloated code.

Spark - not to be confused with Apache Spark - is taking a big step away from these associations by offering a minimalistic framework, focusing on creating web applications rapidly with minimal effort.

As of version 2.0, Spark was rewritten to take full advantage of lambdas introduced in Java 8, further improving the conciseness.

Let's take a look at a simple example.

public class HelloWorld {  
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");

Creating the application using a main method like above will make it run in Jetty. Apart from this, you could also define your application as a filter in the web.xml, allowing you to use the war file wherever you want.

Adding JSON support is fairly easy, and it has a template engine that supports several different types of templates like Freemarker, Mustache and Velocity.

The simplicity of Spark should make every Java developer intrigued. Personally, I just refactored more than 20 webapps to use Spark with my team, and it has been nothing but joy!



Moving over to Scala you'll also find a couple of really nice web application frameworks and Scalatra is one of them.

It's used by many big companies like BBC and Netflix, so it's worth taking a look at.

My team recently had a small internal project ahead of us. It was a great excuse to try something new, so we ended up with a Scalatra web application. I must say, the last week or two has been loads of fun!

In Scalatra you create a set of Servlets.

class HelloServlet extends ScalatraServlet {  
  get("/") {
    "Hello, world!"

Then you mount them to the ServletContext.

class ScalatraBootstrap extends LifeCycle {  
  override def init(context: ServletContext) {
    context.mount(new HelloServlet, "/*")

Even if Scalatra has a simplistic feel to it, it has a great set of features to offer.

Easy JSON conversion with Jackson support, Akka support and great testability are some of the features you'll find.



Finatra is another framework for Scala. The project was started by two engineers at Twitter back in 2012. Now you can find it in production at - yes, you guessed right - Twitter.

Finatra is built on top of two other Twitter products, Twitter-Server - a template of how to build servers at Twitter - and Finagle - a RPC system for creating high-concurrency servers.

Let's start with a simple example.

class HelloWorldController extends Controller {  
  get("/path/to/hello") { request: Request =>
    "Hello World"

Then you add your controller to the server.

class HelloWorldServer extends HttpServer {  
  override def configureHttp(router: HttpRouter): Unit = {
          .add(new HelloWorldController)

There are a lot of cool features to point out here as well.

You'll find support for Mustache templates, Futures and the same easy JSON conversion using Jackson that is found in Scalatra.

On top of that, you'll also find some really handy validation annotations that you can use to validate your JSON.

Here are a few:

case class Account(  
  @Min(0) balance: Int,
  @NonEmpty accountNumber: String,
  @PastDate lastDeposit: Date

Finatra also has an extensive support for dependency injection using Guice. This gives some great advantages when it comes to modularity and testing.



Finally, I want to mention Compojure. It's kind of a stretch to add it to this list since it's just a routing library for Clojure, but I had to add it. I further justify it by adding a link to this excellent article by Adam Bard on Compojure apps, in the style of Sinatra.

Often, if I'm going to write some code in my spare time, I end up using Clojure and Compojure.

Let's take a look at a hello world example.

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))

If you're familiar with Clojure, you've obviously heard about Compojure. However, if you're not familiar with Clojure, then creating a simple Compojure application could be a great introduction.


Missing any frameworks from the list? Let me know in the comments.