CodeX
Published in

CodeX

Developing REST Microservices Simply

Introduction

I am the author of the open-source KISS Web Development Framework described at kissweb.org It is based on Java on the back-end and standard HTML/JavaScript on the front-end. Being frustrated with the unecessary complexity and all of the pieces that have to be fit together in order to create a web application, I created KISS. KISS provides a one-stop shop for everything needed already fully integrated and running out-of-the-box.

KISS includes the front-end, back-end, REST communications, microservices, SQL API, custom HTML tags, authentication, browser cache control, and a lot more. KISS stands for Keep It Simple… To that end, KISS is designed to be as simple to get up and running, simple to learn, and simple to use. In fact, KISS can be downloaded, built, and running in about four command lines!

KISS runs on Linux, Mac, and Windows and is currently being used in a few commercial applications so it is a well-tested, solid framework.

Microservices

Each KISS REST microservice web method is implemented in a single Java method. So, if you have four microservices, you only need four Java methods. A “Hello World” microservice can be fully implemented with a single method containing a single line of code! There is no wiring or configuration. Since they are microservices, simply saving the source file adds the microservice to the system. KISS recognizes new or changed files and compiles them on the fly when they are called. All KISS microservices are fully compiled and run at full speed.

In addition to Java, KISS supports microservices written in Groovy or Lisp as well. Groovy is basically a simplified and enhanced Java. Standard Java can run as Groovy code and Groovy simplifications and enhancements may be used as desired. Groovy code also gets compiled at runtime into regular JVM bytecode so it too runs at full speed.

Lisp support is provided by the ABCL project. It supports full Common Lisp and compiles, on the fly, to JVM bytecode.

In KISS, each microservice is automatically available as a REST web service. No extra coding is needed! KISS REST web services are asynchronous and run in a defined thread pool so that large loads may be handled efficiently.

I am now going to show one hundred percent of the back-end and front-end code needed to define and use a KISS REST microservice.

Example — The Back-end Part

Here is a back-end REST microservice that takes in two numbers from the front-end and returns their sum.

package services;

import org.json.JSONObject;
import org.kissweb.database.Connection;
import org.kissweb.restServer.ProcessServlet;

public class MyJavaService {

public void addNumbers(JSONObject injson, JSONObject outjson, Connection db, ProcessServlet servlet) {
int num1 = injson.getInt("num1");
int num2 = injson.getInt("num2");
outjson.put("num3", num1 + num2);
}
}

That is it. That is all of it. There is no other configuration needed. Except for the three lines inside the method, all of the rest is just boilerplate code. It is the exact same for every REST microservice.

“MyJavaService” is the name of the web service. “addNumbers” is the name of the web method within the web service. The method takes a JSON object that was sent from the front-end and returns a JSON object back to the front-end.

This method expects a JSON object that has two objects named “num1” and “num2”. It gets their integer value, adds them, and returns the sum to the front-end in a JSON object in an element named “num3”. That is all that is needed!

Input and output JSON can be arbitrarily complex or nested. Each web service may contain any number of web methods. Additionally, no error checking is needed. The framework handles errors automatically.

Example — The Front-end Part

Calling the back-end web service from the front-end is equally easy. Also, while the KISS front-end may be used, the KISS front-end is not required. This can and has been used with other front-end frameworks (like React) as well.

Here is all of the code needed to call the above web service from plain JavaScript:

const data = {
num1: 62,
num2: 38
};
let res = await Server.call('services/MyJavaService', 'addNumbers', data);
if (res._Success) {
whatever(res.num3);
}

Another way to call the same services is as follows:

const data = {
num1: 62,
num2: 38
};
Server.call('services/MyJavaService', 'addNumbers', data).then(res => {
if (res._Success) {
whatever(res.num3);
}
}

The call to the back-end automatically handles errors. KISS also supports the ability to call multiple services at the same time, process each as they come in, and continue once they’ve all been processed.

Concluding Remarks

Since KISS web services are microservices, these services can be added, changed, or deleted on a running system. There is no need to bring the server down, rebuild, redeploy, and boot the server back up. The KISS system notices changes and re-compiles changes as needed. Imagine how much easier development is! Imagine being able to correct a bug in a production system without having to bring the system down and disrupt the entire organization!

Another aspect of KISS is authentication. Each of these web service calls is fully authenticated by the framework. You can always be sure of who is making the call and that they are authorized. Also, if you are using HTTPS, these calls are fully encrypted!

KISS is designed to make building web applications as easy as possible. One piece of that system is shown above. KISS includes a lot more! KISS also comes with three manuals and even has a video tutorial series on YouTube. Check it out!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store