Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 1

Matthew Tyson | Oct. 28, 2015
Go extra lightweight, with Java microframeworks Spark, Ninja, and Play

Listing 7. Adding Spark core to the Maven POM



<dependency>

    <groupId>com.sparkjava</groupId>

    <artifactId>spark-core</artifactId>

    <version>2.3</version>

</dependency>

As we did for Ninja, create an Eclipse project (mvn eclipse:eclipse) and import it as described before. After importing the project to Eclipse, you'll notice a significantly smaller number of libraries than you saw for Ninja. This project holds just the core Spark package, some Jetty libraries for the server, two logging jars, and some websocket support libs. Spark is really compact -- and just wait till you see how fast you can start handling requests.

Next, in Eclipse, go to the App.java file that Maven added to our project. Notice that this is actually a Java class with a public static voice main() method -- a standalone executable class. We'll commandeer that for our main class. We can add routes right here in the main class. Update the class as shown in Listing 8.

Listing 8. Adding a Spark route



package org.mtyson;

import spark.Spark;

public class App {

    public static void main( String[] args ){

    	Spark.get("/jw", (req, res) -> "Hello JavaWorld");

    }

}

Now run the app -- in eclipse, you can right-click and select run as, or you can use the alt-shift-x and then the the j key -- and yes, it's really that simple. The above get() method call invokes an embedded Jetty server to host the app. Spark really is micro.

java microframeworksp1 fig6
Figure 6. Executing the Spark main class in Eclipse

Spark request mapping and Java 8 lambdas

Let's take a minute to dissect Spark's mapping from Listing 8. It isn't unlike Ninja's Routes.java file, but it has its own character.

  1. Spark.get() defines the HTTP method.
  2. "/jw" defines the URL path.
  3. You provide the handler.

Items 1 and 2 are probably obvious, but item 3 might be new to you. Since Java 8 is still fairly fresh, and functional programming is one of its coolest new language features, I'll take a moment to introduce this form of the Java lambda expression.

First, take a look at the message signature for the Spark.get() called in Listing 8:

Listing 9. Spark.get() method signature



public static void get(final String path, final Route route)



This method uses the Route object to define the output of the route. Listing 10 shows the Route interface's one and only method (it's important that it be the one and only).

Listing 10. Spark's Route interface



Object handle(Request request, Response response)



Now look back at the arrow (->) lamdba notation in Listing 8. You can see now that what it does it to provide an interface implementation in a single line of code. Nice! This is radical stuff for Java (unless you count anonymous interfaces), but is commonplace in JavaScript and other languages.

 

Previous Page  1  2  3  4  5  6  7  8  9  10  Next Page 

Sign up for CIO Asia eNewsletters.