What are Java micro frameworks and why should you use them?

Java Micro Frameworks

Every language has tradeoffs. With Java, the tradeoff for being a safe, rigorously tested, backwards compatible language is making some sacrifices around agility and streamlining. There’s undeniably some verbosity and bloating, however, the JVM is hugely appealing as a backend if you really want to dive into things or go to high scale. It’s powerful and has been tested in the harshest of environments. Java is widely used and strongly deployable for a reason, after all.

Not everyone is accepting the state of things as they are however. Several people have taken a stab at trying to find ways around the bloat of Java and still keep the power of the JVM. The first wave was around lightweight Java frameworks (a few of which we wrote about: Dropwizard vs. Springboot). These delivered lightweight, modern frameworks for Java and helped cut down development time. Now there’s a second wave that’s been recently hitting the scene. That wave is Java micro frameworks.

What is a Java micro framework?

As a language matures, one unavoidable effect with each release is that well-established libraries grow and swell. After a point, much of the functionality wrapped in those libraries is no longer really needed for most projects. Micro frameworks are an attempt to address this. The idea is to create a focused and agile solution that takes the production process down to its basics.

Micro Java frameworks are essentially a continuation of the lightweight Java frameworks journey. They are the lightest possible way to do Java today. As the new kids on the block, they haven’t been deployed across every type of production environment yet, but the early production deployments have shown strong success. Their appeal is clear and there’s a lot of excitement and active development in this space right now.

Today, I’m taking a look at three of the more popular Java micro frameworks available: Ninja Framework, Spark, and Jodd.

Java micro frameworks Github
Java micro frameworks – Github stats

Ninja Framework

Ninja framework
Ninja is a full stack micro Java framework for creating webservices and applications. It aims to take the positive aspects of Play and Ruby on Rails and deliver them through a pure Java format. It uses an MVC concept in order to facilitate simplicity and the ability to write applications of any size in a consistent manner.

As an integrated software stack, Ninja comes with an extensive amount of functionality out of the box (e.g. JSON parsing, metrics support, HTML rendering), which requires some annotations and definitions for your controllers. Much of these functionalities are based around making Ninja a DevOps-friendly tool. The inclusion of these libraries make Ninja easy to use. On the scalability front, Ninja is a stateless design, which makes horizontal scaling just a matter of adding servers.

The tradeoff for the functionality and ease of use is less user choice and a larger footprint than some other tools in this space. The structure for development is chosen for you and a set of source folders and files are created automatically.

Popularity and Libraries: On Github, Ninja has 809 stars and 288 forks as of this writing, which is comparable to Jodd, but well behind Spark in popularity. If you want to contribute, check out the Github page. For HTML rendering, Ninja uses Freemarker. For handling JSON and XML, Jackson is used. Many of the core libraries are written by Ninja.

Getting Started: Getting started with Ninja is fairly straightforward. It’s based in Maven, requiring Java and Maven to run. Ninja projects work through Maven archetypes, which allow users to build new projects with a single command once they’re built. To kick off your first project, the command is


mvn archetype:generate -DarchetypeGroupId=org.ninjaframework -DarchetypeArtifactId=ninja-servlet-archetype-simple

Once you enter a few values and move into your project directory, executing the following will start Ninja in its “SuperDevMode” (which picks up changes you make automatically):


cd MY_INSTALLED_PROJECT
mvn clean install // to generate the compiled classes the first time
mvn ninja:run // to start Ninja's SuperDevMode

From there, you import your project into your IDE of choice and start coding. For further details, check out the documentation.

Takeaway: Ninja Framework redesigned the Java framework to deliver something that is simple, scalable, and easy to use. It delivers a lot of functionality that makes it easy for a user to get up and running quickly. As a full stack framework, you do get a larger footprint and less customizability than with some other options.

Spark

Spark
Not to be confused with Apache Spark, the goal of this Spark was to design a pure Java framework that could be used for easily and simply creating REST APIs and/or web applications. At less than 1 MB in size, Spark is an agile and slimmed down framework that can be used for quickly and easily developing applications. Initially inspired by Sinatra, Spark has grown into a fully realized web framework with an engaged developer community that continues to maintain an active release cycle.

As a micro framework, Spark made the decision to let users pick their template engine(s) for designing their application. The flexibility they provide for choice of libraries allows for users to choose the best options for their projects without any lock-in or learning curve difficulties for ecosystem particulars. And since they’re focused on delivering a small footprint solution, there’s little configuration or boilerplate required.

The tradeoff for the flexibility and simplicity of Spark is less functionality than other options out there. Spark aims to be as streamlined and straightforward as it can, stripping out the need for many extra efforts like excessive annotations and XML configurations. In order to be super lightweight, it sacrifices some potentially useful functionalities.

Getting Started: Getting started in Spark is extremely simple. It requires Java and Maven to run. The first step is adding a Maven dependency:

<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.2</version>
</dependency>

The second step is to import Spark and start coding:


import static spark.Spark.*;
public class HelloWorld {
    public static void main(String[] args) {
        get("/hello", (request, response) - > "Hello World!");
    }
}

For additional details, check out the documentation.

Popularity and Libraries: On Github, Spark has 2,398 stars and 542 forks as of writing, making it easily the most popular of the three frameworks I’m spotlighting here today. If you want to contribute, check out the Github page. Part of the flexibility Spark offers is not locking users into specific libraries. For example, for HTML rendering, you could use Freemarker, Mustaches, Velocity, Jade, Handlebars, Pebble, or Water. For whichever you want to use, add a Maven dependency to set it up.

Takeaway: Spark strips out a lot of Java bloat and delivers a minimalistic and flexible web framework that is simple to set up and can be used to create a wide range of Java projects. By slimming way down however, you do lose some functionality, so Spark isn’t ideal for large scale web applications.

Jodd

Jodd

Jodd is a collection of micro frameworks, tools, and utilities for Java, so it’s perhaps not directly comparable to some of the others here. In fact, Spark could likely be included as a component of Jodd for example. But the driver for Jodd is to reduce the footprint and slowness of many Java tools and frameworks, and deliver a selection of super lightweight frameworks and tools. Rather than group them all together in one monolith application, Jodd breaks each library down into its own individual piece, which allows the user to pick and choose which ones they use.

Jodd has been around for a while, and over that time they’ve built lightweight versions of tools and utilities whenever they’ve noticed a need. By keeping the tools and frameworks separate and delivering them as a loose bundle, Jodd can keep extremely focused scopes for each individual piece. They have frameworks for handling requests, for DI containers, for parsing HTML5, and so on. A collection like this can seem to go against the idea of streamlined micro frameworks, but the entire bundle comes in at under 1.5 MB, so the footprint is small.

The tradeoff with Jodd is that it’s not done for you. The functionality for most things is there in the collection of libraries, but you have to get your hands dirty and get it set up yourself. The customizability provides a lot of control and flexibility for the user, but that comes with less ease of use. Fortunately, each individual tool is simple and tiny, so diving into the code doesn’t require wading through a sea of complexity. The documentation on the Jodd site provides some guidance and best practices as well.

Getting Started: Jodd is built with Gradle and is targeted to Java 1.5 and above. As Jodd is a collection of tools, getting started is not as straightforward as other tools. One option is to download the Jodd bundle to get all the modules. Another option is to build Jodd from the source through Git. To do so,
Clone the Jodd Git repo:


git clone https://github.com/oblac/jodd.git jodd

then build the project with:

gradlew build

You can also install Jodd in Maven. To do so, use the command:


gradlew install

For more details about the different modules and some examples on how you can use them, check out Jodd’s documentation.

Popularity and Libraries: On Github, Jodd has 837 stars and 243 forks as of writing, making it comparable in popularity to Ninja, but well behind Spark. If you want to contribute, check out the Github page. Jodd is a collection of utilities, frameworks, and tools, so the libraries it uses are its own.

Takeaway: Jodd is a collection of very focused and lightweight frameworks and tools. Users can combine these libraries in a huge number of ways to get customized solutions for their needs that still deliver a lot of functionality. By breaking down each element into a different piece however, the onus is on the user to dive in and get everything set up the way they need it.

Conclusion

Java micro frameworks are an attempt to address the weaknesses of Java while maintaining its strengths. Their focused, bare bones approach allows for very light application deployment, helping developers combine the power of the JVM with the agility of newer, faster languages.

As a newer development, defining what qualifies as a Java micro frameworks can be a matter of opinion. Depending on how you want to define the term, there are several others you can include and explore, such as JHipster, Javalite, Ratpack, and Vert.x. If you’ve worked with micro frameworks in the past, please let us know your opinions on them in the comments section below.

Further reading: 

blog_boat (1)

The 7 Log Management Tools Java Developers Should Know

During his time at OverOps, Josh was in charge of product marketing. He wrote and maintained product documentation in addition to his contributions to the OverOps Blog. He's a big baseball fan and a small beer nerd.