If you're writing a web application from scratch, you'll want to select a framework to make your life easier and reduce development time. Java, one of the most popular programming languages out there, offers plenty of options.
Traditional Java applications, particularly web-facing apps, are built on top of a Model-View-Controller (MVC) framework, which follows the MVC software architectural pattern. Starting with Apache Struts, MVC frameworks have been a staple of Java development including such popular frameworks as WebWork, Spring MVC, Wicket, and GWT. Typically these applications host the view code on the server, where it is rendered and delivered to the client (web browser). Click a link or submit a form in your browser and it submits a request to the server, which does the requested work and builds a new view, refreshing the entire display in your client.
Fast-forward to 2015, and most Java applications (particularly enterprise applications) still follow this same model. This is not the dominant model on the web, however, where speedy and responsive user interfaces are expected, save buttons are optional, and mobile accessibility is required. Today the popular software architectures are service-oriented with much recent discussion around microservices—individual services which perform only one small unit of work and can scale (increase capacity) separately from each other function of a system.
Fortunately, the Java community is well aware of these changes and new frameworks and paradigms exist. We will explore just four of these: Spring, Play, Dropwizard, and Spark.
Criteria
In selecting which frameworks to discuss, several criteria were considered:
- Released under an OSI-approved open source license.
- Should be quick to get a project off the ground and produce prototypes.
- Needs to integrate into a container such as Heroku or Docker for easier deployment.
- Painlessly supports service-oriented and microservice architecture.
- Must support the Twelve-Factor App methodology.
The Twelve-Factor App is a document created by the founders of Heroku that describes the features that empower an application to achieve web scale through management of the hosting environment. Following the Twelve-Factor methodology is a great way to start building apps for the modern web and break out of the traditional enterprise-model of Java applications.
Each of the frameworks provides the basics in a different way, summarized below:
Spring (Boot) | Play | Dropwizard | Spark | |
License | Apache 2 | Apache 2 | Apache 2 | Apache 2 |
HTTP | Undertow1 | Netty | Jetty | Jetty |
REST | Spring MVC2 | Play5 | Jersey | Spark5 |
JSON | Jackson3 | Jackson | Jackson | Gson |
Templates | JSP4 | Twirl | Mustache4 | Mustache4 |
WebSockets | Spring API5 | Play5 | via Jetty | via Jetty |
DB/ORM | Spring Data5 | Ebean | JDBI | [MyBatis]6 |
Auth | Spring Security5 | Play5 | Dropwizard5 | [Shiro]6 |
1 Spring Boot also supports Tomcat and Jetty.
2 Spring also supports JAX-RS using Jersey.
3 Spring can be configured to use Gson instead of Jackson.
4 Other available template engines may include Freemarker, Velocity, Thymeleaf, Tiles, Handlebars, and Jade.
5 This feature is built-in to the framework.
6 As a lightweight framework Spark doesn’t come with database or auth components, however other lightweight, single-purpose, 3rd components under the Apache 2 license make sense with this framework
Spring
First released in 2004, Spring is the oldest framework on our list. It is the most popular Java framework is nearly synonymous with Java development in enterprise circles. As it expanded to include a full MVC framework it has continued to adapt to change and is a true full-service framework providing services for security, web services, database integrations, and more.
One of the latest improvements includes Spring Boot, which provides the configuration and tooling necessary to get an application off the ground quickly and easily create stand-alone applications.
While popular, Spring is not without its downsides. For many applications Spring all-things-to-all-people approach may add an unnecessary layer of complexity, particularly compared with Java Enterprise Edition which includes many core features of Spring in its latest versions.
Play
The Play Framework (not to be confused with Google Play) is the second on our list in terms of both popularity and tenure. It was conceived in 2007 as a web MVC framework supporting REST web services as a first-class citizen. In addition to Java, Play supports Scala, an object-oriented functional language on the Java virtual machine.
Other important distinctions from other frameworks include asynchronous I/O, stateless services, and first-class JSON support. Developer productivity is improved in part due to immediate code refresh, which doesn't require republishing or restarting.
There are some potential downsides, however. Play chose a complete break from the traditional Java enterprise paradigms such as the Servlet specification. This may render some 3rd party libraries unusable. Also, some Java developers complain about the strong Scala influence of Play which has changed the mental model required to use the framework.
Dropwizard
Like Spring Boot and Play, Dropwizard is focused on rapid development and deployment of Java web applications and REST services. While it provides some limited functionality of its own (such as authentication), mostly Dropwizard focuses on the "glue" between components to provide a more integrated development experience. It is designed to be a simple, lightweight framework with everything you need to deploy your Java project.
Dropwizard is still in active development and has not yet attained its 1.0 release. However, it is based around some very stable, best-of-breed libraries that have independently achieved stability and maturity.
Spark
Spark started in 2011 as a forward-thinking framework based upon simplicity and modeled after Ruby’s framework. As of Spark 2.0 released in 2014, the framework only supports Java 8. Spark Java should not be confused with Apache Spark, a big data engine.
The Spark quick start is a "Hello World" web server that takes just 6 lines of code. It is built for REST web services and is much simpler than typical Java solutions such as JAX-RS. The focus is on developer productivity, and the extremely lightweight framework provides just what is necessary and no more.
The weakness of Spark, as with any other micro-framework, is in the details it ignores. Considerations such as security, database connectivity, etc. are left to the developer to handle with some other framework or library.
Selection
Selecting the right framework can be challenging, and every organization applies their own preferences to the decision-making process. Does it make more sense to have an all-inclusive framework or a lighter-weight, less-opinionated one that allows you to choose your own libraries? Is your team willing to learn a new system? What is the tolerance for using pre-release or beta software?
Whatever framework you choose, consider the license, community activity and support for the project, and the stability of the finished product. Good luck, and happy coding.
9 Comments