The Personal Blog of Todd Sharp

Getting Started With The Spark Java Framework

Posted By: Todd Sharp on 4/4/2017 1:58 GMT
Tagged: Groovy, Groovy On Raspberry Pi, Raspberry Pi, Spark Java

I published a post last week showing how to use Grails to create a website on the Raspberry Pi.  After some feedback and conversations about whether this was "overkill" for a simple Raspberry Pi website I decided to revisit the topic and see how I could simplify things a bit without sacrificing the power of Groovy and Pi4J. I've done a bit of digging and discovered the Spark Java framework.  

Although this post refers to deploying on Raspberry Pi, it should be noted that it isn't limited to Raspberry Pi.  Spark Java is a simple and lightweight framework that can be used for any web application.

So what is Spark Java?  From the Spark Java site:

Spark Framework is a simple and lightweight Java web framework built for rapid development. Spark was originally inspired by the web framework Sinatra, but it's intention isn't to compete with Sinatra, or other similar web frameworks in different languages. Sparks intention is to provide a pure Java alternative for developers that want to (or are required to), develop their web application in Java. Spark is built around Java 8's lambda philosophy, which makes a typical Spark application a lot less verbose than most application written in other Java web frameworks.
Spark focuses on being as simple and straight-forward as possible, without the need for cumbersome (XML) configuration, to enable very fast web application development in pure Java with minimal effort. It’s a totally different paradigm when compared to the overuse of annotations for accomplishing pretty trivial stuff seen in other web frameworks.

Sounds like a perfect framework for the Pi.  Simple, self-contained, lightweight and convention based.  No need to install a web server like Tomcat or Apache, no need for a full blown framework like Grails.  And since Groovy closures can be used as Lambda expressions there is nothing stopping us from using Groovy instead of Java with Spark Java.  So, let's do that.

Create a new Gradle project in IntelliJ.  Once you've got your project created, open up your build.gradle script and paste the following:

We declare three dependencies here:

  1. Groovy
  2. The Spark Java Framework
  3. Pi4J (we'll get to that in another post)
The runServer task is what we'll use to launch the server via Gradle.  Next we need a simple Bootstrap script which is what we'll need to declare our routes within the Spark Java application (basically - one route for each verb, if needed, for each path within the application).

Note:  Your Groovy files will need to be under src/main/groovy or Gradle won't find them!

Now populate your src/main/groovy/Bootstrap.groovy class as such:

And sync the code with your Pi.  

Check this post to see how I sync with my Pi.  You can always use SCP or FTP as desired.

You're now ready to launch the app.  In an SSH session navigate to where the build.gradle script is and run gradle runServer.  The first time you run it will take a bit longer than subsequent runs, since the dependencies need to be downloaded.  Eventually you'll see the following in your console:

runServer[Thread-1] INFO org.eclipse.jetty.util.log - Logging initialized @1898ms
[Thread-1] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
[Thread-1] INFO spark.webserver.JettySparkServer - >> Listening on
[Thread-1] INFO org.eclipse.jetty.server.Server - jetty-9.3.2.v20150730
[Thread-1] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@2ad3bd{HTTP/1.1,[http/1.1]}{}
[Thread-1] INFO org.eclipse.jetty.server.Server - Started @2319ms

And that's it!  Your site is now up and running.  Try it out by hitting http://[Raspberry Pi or localhost IP]:4567/hello and you should see the message 'Hello World' in your browser.  

Image by cocoparisienne from Pixabay

Related Posts

Simulating Texas Hold'em With Groovy

Simulating Texas Hold'em With Groovy

Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy.  My goal wasn't...

Intro To Oracle Application Container Cloud Services

Intro To Oracle Application Container Cloud Services

I've long been an AWS user, but recently one of my projects at work has expressed some interest in getting our platform running on multiple cloud service...

Creating A Slack Clone With Twilio, Angular 5 And Electron

Creating A Slack Clone With Twilio, Angular 5 And Electron

I've heard plenty about Twilio over the past few years and had always wanted to learn more about their APIs.  They have a ton of different products...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!