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


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.




Running the Service

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

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


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

VS Code — Generated Sequence Diagram for “lookupStudent” Resource




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