; last updated - 5 minutes read

WebServices and REST services have come a long way in Java. Nowadays, it's very easy to create a webservice. Basically, it's adding an annotation to a method. All you need is a servlet container, such as Tomcat. But then, configuring and running a servlet container isn't really simple. Chances are you think it's simple, but that's because most Java programmers have been using application servers for years. They already carry everything they need in their toolbox. But from a newbie's point of view, things look a little different.

Spark is a nice alternative making it really easy to write a REST service:

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

That's all you need to implement a web server that serves a single REST service. Start the program, open your browser and enter the URL to test your REST service:

http://localhost:4567/hello

Notice that this is a main method. You don't have to deploy a war file. Instead, Spark follows the idea popularized by Spring Boot: it embeds Jetty in a jar file. When I tested the code, Spark took virtually no time to start. That's the nice thing about embedding Jetty: it has a really small footprint. I started to use an embedded Jetty for my web applications years ago. It's a really fast alternative if you don't need a full-blown application server.

How come Spark is so simple?

Spark is simple because it doesn't have to be a full-blown application server. Under the hood, there's Jetty, so Spark can be used as a web server serving static files. And it can be used to write REST services. But it's not meant to run JSF pages, for instance (even though I suppose it's possible).

Another key to simplicity is Java 8. The get method shown above takes two parameters:

  • The URI of the REST service (more precisely: the path of the service)
  • and a lambda expression.

The lambda expression is the REST service itself. Before Java 8, there was no elegant way to pass a method as a parameter. Anonymous inner classes were considered cumbersome to most developers, so they never become popular. In any case, anonymous inner classes consist of a lot of boilerplate code, so the one-liner above would have become a multi-line method call that's far from being easy to read:

get("/", new Route() { @Override public Object handle(Request request, Response response) { return "Hello World!!"; } });

I've found this example in this article on Spark, MongoDB and AngularJS..

That's why annotations have become so popular. At the time, annotations simplified things a lot, so there's nothing wrong with that. However, I've come to believe annotation would never have become as popular if lambda expressions had been added to Java earlier. The get method above is an example. It's almost as readable as the annotation @Get("/hello"), but it has two important advantages: it can be debugged, and it doesn't rely on the magic of an external framework. Annotation-processing frameworks are nice as long as you use them correctly, but many people don't feel comfortable with annotations because they can't see what's happening. The annotation processor is decoupled from your business code, which is a good thing from an architect's point of view, but bad when you're trying to hunt down bugs in a hurry.

Let's continue with the tutorial

By the way, in case you wonder why I started to ponder about annotations and lambdas instead of continuing with the tutorial - well, putting it in a nutshell, you've already seen everything that's important. Of course, Spark can do more than just starting a server on the default port and have it serve a single GET request, but the rest of the API is pretty obvious.

It's almost needless to say that Spark also offers post(), delete(), put() and options() to serve the corresponding REST requests. There are also methods to implement filtering, exception mapping and to transform objects to Json. Most of the remaining API should be familiar to your from the servlet API. In any case, read the manual - it's really short, it's only a five minute read.

License

Spark has been put under an Apache V2 license (the same license PrimeFaces uses).

Wrapping it up

Spark is an interesting alternative to the popular annotation-based approaches to writing REST services. It shows a glimpse of how Java might have evolved if lambda expressions had been added to the language prior to annotations. In any case, it's a slim and interesting alternative to providing REST services on a heavyweight application server.


Dig deeper

Spark project page

Developing single page applications Spark, MongoDB and AngularJS (note that the web page uses eight trackers, so if you care about your privacy, use a tool like Ghostery)


Comments