ivanursul

Software engineer

Spark - Micro Web Framework for creating web applications with minimal effort

Today I am going to write few words about Spark framework - micro framework for rapid web application development.

Requirements

Navigate to your project desired location and execute in terminal:

mvn archetype:generate -DgroupId=org.ivanursul.spark -DartifactId=spark-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Then open your IDE and import maven project

Adding dependency

To include required libraries just add one dependency

<dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-core</artifactId> <version>2.1</version> </dependency>

Don’t forget to add maven-compiler-plugin

Now you are ready to write some code.

Official site says, that it is enough to write something like

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

and after execution of program you car test this app by typing

http://localhost:4567/hello Seems to be very easy, but let’s try something harder.

Implementing CRUD operations

To implement CRUD(create, remove, update, delete) for, say, person entity, we will gonna need:

DTO(Person) Serializer/Deserializer(GSON? :) ) Note, that I will do it as simple, as I can, without any structures, just for testing this framework.

Let’s start

Add gson dependency

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency>

Create person class

Create main class

That seems to be all, that is needed.

What did we made ?

How to test?

If you have Postman extension for chrome, you can try to import my collection for this tutorial

https://www.getpostman.com/collections/071a319d71ab1a639c69

Impressions

Really, I see, that this framework is very easy to learn, easy to start, but not easy to maintain.

For quick POC’s this framework is quite good, but for something really big and scalable - I think not.

The problem will occur, when I will try to change something, then I will notice, that there are static fields, classes, everywhere, and It will be very hard to maintain such code.

Resources

You can find github project here.