Sparkjava & JDBI

Once you spend a lot of time with a set of tools there is a tendency to come up with solutions to every problem with just those tools. This narrow tunnel vision is dangerous for a techie since you can be completely blindsided when something new comes up and you are found lacking in new skills. It also inhibits the ability to learn new things and take in new ideas. Having spent a lot of time in the Java Spring tunnel, it was a welcome break for me to try out SparkJava & JDBI recently – void of any Spring, JEE or IoC.

Its not that I do not like Spring. I am a consistent Spring user and will be using it for the foreseeable future. The agility and speed with which they adapt is what will keep me and many others using that. But sometimes it is good to step away and look at other options.

Why SparkJava? I must confess, I am a Java junkie. Been doing that for too long now to even keep track. But I often wish I had the ability to spin up services with the same ease as in other tools. Take a look at ExpressJS and Sinatra if you don’t believe me. With ExpressJS you will be shocked to see how simple it is to develop RESTful services. It is almost intuitive. There I said that.

This is ExpressJS code for something I put together a while back…

That is all it took to stand a RESTful service at endpoint http://hostname/candidate/:name where ‘name’ is the name of a person. The other classes are related to retrieving data from a mongo database.

Then there is Sinatra….

Now if only we could do this in Java. Enter Spark (not to be confused with the other Apache Spark which is around large scale data processing). Spark calls itself a micro web framework. While I am not sure what the formal definition of this would be, here is what I think it means – “product that does one thing well and really really well”.

Here is our evergreen hello world with Spark. Run this Java class and go to http://localhost:4567/hello/thisissparkjava

Now in any application you will also need database access. In comes JDBI. JDBI uses an API style that is referred to as fluent style. Working with fluent API’s is a pleasure IMO.

I have organized the sample project (in github) in a manner that allows routes to be defined in domain specific classes and wire everything together in the Application class. So run the Application class and you will have a RESTful note taker service. Add a note, get a specific note or get all notes. The in-memory H2 database connection pool is defined in the DB class.

Application.java defines the routes using specific route classes that all implement my RouteRegistrar interface.

Sample route from NotesRoutes…

The full source is available on my github repo at – https://github.com/thomasma/sparkjdbi

Run Application from your favorite IDE. Use your favorite RESTful client to invoke one of the following:

  • To get all notes:  curl http://localhost:4567/note
  • To get specific note: curl http://localhost:4567/note/123 (123 is the note id)
  • To add a new note, submit request: curl -XPOST http://localhost:4567/note -d ‘{“note”: “reminder to learn more spark”}’

In conclusion, this sample shows how simple it is to create database driven RESTful services using Spark, JDBI and plain Java. For a production application I would probably factor out the database access code into a separate set of classes for better design flexibility. For security we can use before filters to add authentication/authorization controls very easily.