Developing REST Microservices Simply

Blake McBride
Aug 9 · 4 min read

Introduction

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

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

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

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

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!

CodeX

Everything connected with Tech & Code. Follow to join our 500K+ monthly readers