Converting a Spring Boot Service To Ballerina

  • Student record lookup returns a JSON student representation with HTTP 200 OK or an HTTP 404 Not Found if the record is not there.
  • Student record insertion returns a HTTP 200 OK or else if the record is already there, should return an HTTP 400 Bad Request.
  • Student record update returns HTTP 200 OK on success, or else, if the record is already there, should return an HTTP 400 Bad Request.
  • Student record delete will remove the record if it exists, and will always return HTTP 200 OK.
  • The student insertion and update schema must be validated, and the request should fail for an invalid input. The student “major” field is an enumeration with three values: “CS”, “Physics”, and “Chemistry”, and it has the default value “CS”.

Spring Boot Implementation

We will be using Gradle to setup and build our Java project. The following build.gradle file contains the basic configuration to setup a typical Spring Boot service application.

build.gradle

Application

This is the entry point to the Java application, which contains the main method. This will bootstrap the Spring application by locating the service implementations in the classpath and initialize the required components.

https://gist.github.com/lafernando/8ec0ea1f1b9dcf1d76669c7f3079e883Application.java

Student

This is a model class to represent the resource information used in the service.

Student.java

StudentRegistryController

This class represents the resource controller which is used as the target for where the messages are dispatched to. It will contain annotations which maps request patterns to methods.

StudentRegistryController.java

Running the Service

With the above source files available in our Java package, we can build the project by executing “gradle build” in the base directory. The jar file that is created contains all the dependencies and the service implementations added. We can now simply run the generated executable jar file with the following command.

Student Registry Spring Boot Application Startup

Ballerina Implementation

Ballerina has two entry points to its applications. It can either be a main function, or a service. For our use-case, a service is directly applicable. Let’s look at the full Ballerina implementation, and how to build and run the service.

student_registry.bal

Resource Model Representation

The resource model implementation in the Spring Boot application, done using the “Student” Java bean class, is done using the “Student” record type in Ballerina. Since this is a structure that simply needs only data and no behavior, the record type is the exact candidate for this. Also, a new type “Major” is defined using constant values to represent the possible major values for the student. One of these values are also given as a default value the the “major” field in the “Student” record type. The default values, along with other record type level concepts such as, optional fields and values, are directly handled by the data binding operations of Ballerina services.

Application and REST Controller

The Spring Boot application and the REST controller is represented by the Ballerina service construct. A Ballerina service represents a network service, which is bound to a specific type of a network listener. Here, we have used an HTTP listener, but this can also be other types of network listeners, such as gRPC, WebSocket and so on.

Graphical View

In Ballerina, an additional aspect that is covered is the auto generation of the sequence diagrams for the code. This is possible, since the Ballerina language itself is designed ground-up to be based on a sequence diagramming concept. Here, all our clients, listeners, remote endpoints are visualized as actors in the diagram, and its message passing between them are shown using remote function calls, denoted using the “->” operator.

VS Code — Generated Sequence Diagram for “lookupStudent” Resource

Summary

The above shows a scenario on how to convert a typical Spring Boot HTTP service application to a Ballerina service. The full source code for the Spring Boot application can be found here [1], and the corresponding Ballerina code can be found at [2].

--

--

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
Anjana Fernando

Anjana Fernando

Author of ‘Beginning Ballerina Programming’ | Software Developer | Evangelist | 🇱🇰 🇺🇸 | Working at WSO2 Inc. — @lafernando