Converting a Spring Boot Service To Ballerina

There seem to be increasing interest for people in knowing how to migrate a Spring Boot application to Ballerina. Spring Boot has been a very popular platform for writing HTTP/RESTful services; and then came along Ballerina, which is introduced as a programming language that is specialized in writing network-aware applications, thus promising much more productive user experience for the developer. With these new additions, many are curious to know how to possibly rewrite their Spring Boot applications in Ballerina to get the long term benefits.

In this post, I will dissect a Spring Boot application, and show how their concepts are mapped to Ballerina programs. The sample use case I’ll be implementing is based on a student registry scenario. An in-memory registry is kept to insert, lookup, update, and delete student records. The interface for this registry is through an HTTP API. In order to cover the most often used functionality in writing similar services, I’ve added the following requirements to the use case.

  • 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”.

Let’s first start off with the Spring Boot implementation.

Spring Boot Implementation

build.gradle

Create a directory named “student_registry”, and drop in the above file. Inside this directory, also create the directory structure “src/main/java/org/demo”. In Unix-like systems you can use the following command.

mkdir -p src/main/java/org/demo

Inside the “org.demo” Java package, we will be having three classes, these are introduced below.

Application

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

Student

Student.java

StudentRegistryController

StudentRegistryController.java

Running the Service

java -jar build/libs/student-registry-service-0.1.0.jar

Image for post
Image for post
Student Registry Spring Boot Application Startup

The execution starts up its own embedded web server and start serving requests to the service.

A sample session with the service is shown below.

In order to view the return HTTP status code, the curl command can be given the “-v” switch to enable verbose mode.

In the next section, let’s see how we can implement the same with a Ballerina service.

Ballerina Implementation

student_registry.bal

The above code can be built and run using the following commands.

$ ballerina build student_registry.bal

$ ballerina run student_registry.jar

The Ballerina service implementation is similar in functionality to the earlier defined Spring Boot application, thus, we can use the same HTTP requests in testing the service.

Now let’s analyze the code to see how each aspect of the Spring Boot service is fulfilled by the Ballerina service.

Resource Model Representation

Application and REST Controller

Individual resource functions represents the service mappings of incoming individual requests. These are similar to the Spring Boot REST Controller methods. Service and resource level annotations are used to provide additional metadata on the services, such as base paths, resource paths, and supported HTTP methods.

Also, in Ballerina, we have access to the incoming request caller, in order to send explicit responses back to the client — which is done using caller->respond(). This has the added advantage of the user having full control of the return communication back to the client. For example, if there is an error in the response communication, this is directly seen and accessible in the Ballerina code, thus we can provide additional logic to handle this scenario. In the Spring Boot code, we return a Java object, or we throw an exception to signal the return communication to the client. And thereafter, we do not directly have access to the return communication back to the client.

Graphical View

Image for post
Image for post
VS Code — Generated Sequence Diagram for “lookupStudent” Resource

Summary

[1] https://github.com/lafernando/samples/tree/master/springboot/student_registry

[2] https://github.com/lafernando/samples/tree/master/ballerina/student_registry

Author of ‘Beginning Ballerina Programming’ | Software Developer | Evangelist | 🇱🇰 🇺🇸 | Director — Developer Relations @wso2 — @lafernando

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