Simple Guide to Spark Framework for Microservices

Every modern enterprise application is supported by microservices. These services form the core of the application. To develop these services fast we need an easy and simple framework. In this article, we will have a brief introduction to Spark Framework.

As claimed on the official site for Spark.

Spark – A micro framework for creating web applications in Kotlin and Java 8 with minimal effort

Software used

  • Java 8
  • Spark
  • Maven
  • Eclipse

Using the above tool we will see how Spark Framework can be used to develop some basic services.

Maven Dependencies

The dependency spark-core is what we need to run the complete Spark Web Framework. As we may use some JSON responses during our example so we used GSON library.

Routes

Routes are the main building block in this framework. Each route is made up of 3 basic elements – verb, a path, and a callback.

The Verb is associated with each of the HTTP methods. These are get, post, put, delete, head, trace, connect, options.

The Path or Route Pattern determines which URI(s) the route should listen to and provide a response.

The Callback is a is a handler function. This function takes two important parameters i.e. request and response objects.

Routes are matched in the order they are defined. The first route that matches the request is invoked. A sample route is like:

Hello World! Service

We will see below a simple and straight forward API developed using Spark Framework. You simply have to provide a main class as below:

Spark recommends importing the methods statically to ensure good readability.

The first methods configure the Spark embedded server to run on port 8080. The server is automatically started when you do something that requires the server to be started (i.e. declaring a route or setting the port).

The first route is simple and static. The second route is dynamic as it has a placeholder for the name variable. The lambda callback has access to Request and Response objects and can access information out of it.

Testing the Service

Once you run the main class, the Spark server is started and its available on port 8080. You can test the above set routes in the postman.

 

RESTful Service with Spark Framework

We will shortly look on how we can implement a simple RESTful API with this framework.

We have a simple domain object around which we will write the API

We are not connecting to any database. For our demonstration purpose, we will have some static map that will hold data for us. Below is our service interface:

Add a User

To add a user in our API we will define Spark route as:

The postman request is:

Get All Users

As we have added users, we need to check if that is available in our system. The get route we have added is:

This route will return all the users in the system. The postman output:

 Edit a User

Editing a user is to replace the user with new information. The route is defined as:

Delete a User

In delete route, we are simply removing the element from Map:

All these routes can be tested in POSTMAN.

Conclusion

The article takes us through a quick introduction to the Spark framework for rapid web development.

This framework can be used for generating microservices in Java.

You can download the complete code from our GIT Repo

Download Code

Add a Comment

Your email address will not be published. Required fields are marked *