Ballerina REST API service with MySQL connector

Chamindu Udakara
5 min readMar 30, 2021

What is this new thing called “BALLERINA”????

Ballerina is the new dancer recently introduced by WSO2 to the battleground of new cloud-era programming languages. This is an open-source programming language and also a platform that can be used easily to code cloud-based applications with minimal effort.
It is also a comprehensive platform, including extensive libraries. That’s just a basic pitch about this new dancer.

How to develop REST API service with “Ballerina”??

Here is the actual question that we need to answer.

Can this new dancer facilitate developing HTTP network communication???

The answer would be YES, OF COURSE. Ballerina supports not just HTTP network communication. It supports HTTP, gRPC, and Websocket communication as well. This is just great. Now we can move on to the second part of the question.

How we can write a REST-based HTTP server with Ballerina????

Let’s just dig into the task.

  1. First of all, download the ballerina latest release (Ballerina SWAN LAKE Alpha 2) and set it up on your machine. (https://ballerina.io/learn/user-guide/getting-started/setting-up-ballerina/)
  2. Once you finished setting up install the VS code plugin for Ballerina. (https://ballerina.io/learn/user-guide/getting-started/setting-up-ballerina/#installing-the-vscode-extension)

Now you are ready to start the dance to create this HTTP service.

  1. In the command line, execute the bal new RESTService command to create a new ballerina package. And it will generate a package with the following directory structure. This your product package.
Directory structure

2. Then rename this main.bal file as the server. bal.

3. The example REST API that we are going to develop is something like this,

How to structure an HTTP Service in Ballerina??

The following diagram will explain all the things you need to know when defining HTTP services using Ballerina.

4. Let’s implement these resources with Ballerina. For the initial phase let’s just return messages with the method descriptions.

5. Now, let’s run this server and invoke the get method with a fake id to check this service. To run the service, execute the bal run RESTService command in the CMD. Then you will see the following output.

6. Let’s invoke this REST API service as a client to check whether this simple code was able to deliver the task as we expected. Execute the following command curl --location --request GET 'localhost:8081/sample/getData/1'.

Result of the REST API Method invocation.

Now we know that it is pretty easy to create an HTTP server with this new dancer. But the actual goal is not just to start an HTTP server and invoke it, but to use this service to add data to a MySQL database and retrieve those. It is what we are going to try in the next phase. Giddy up guys!!!

Connecting the MySQL database with the implemented REST Service.

Ballerina can integrate different types of Databases. This is something we expect with new cloud-era programming languages. Ballerina facilitates this requirement making a strong case as a competitor on the battlefield. Before we start, we need to download the latest MySQL connector and extract it from the machine.

  1. Once you have downloaded this connector you need to add this dependency to your package like this.

2. Now you have added the MySQL database as the data source for the REST API service of yours. Let’s create the SQL.bal file to add data to this database and get data from the database.

3. First of all, we need to initialize a client and create a database if not a Database exists. Then create a sample table and initialize them in the main method when the server is starting.

4. Implement Methods to get a Data item from Database and a method to add a new method to the database. (the following code snippet is an example for the 2, 3, and 4 steps)

Voila! Now the MySql connector and data add and retrieve processes are established. Now we need to link the HTTP resources to these methods. We can do that by simply updating the server.bal file as follows.

lets

Now your HTTP server is implemented with a Mysql database connection. Now let’s check whether our REST API is working as we expect.

The easy and satisfying phase…

  1. Execute the bal run RESTService command in the CMD. Then you will see your service is running.
  2. Let’s invoke the POST method with Payload data. For that use this CURL request,curl --location --request POST 'localhost:8081/sample/AddData/1' --header 'Content-Type: text/plain' -d '{"description": "Item 3"}'.
  3. You will get the following response which is what we expect.
The response to the POST request.

4. Then, let's invoke the Get request with item number 4. Use this CURL request for that, curl --location --request GET 'localhost:8081/sample/getData/4'

The response to the GET request.

And yeah that’s it! Hope you got an understanding on working with the new dancer “Ballerina” to create an HTTP/REST Service! 😁 Don’t forget to go through the Ballerina website and dance different styles with the feature offered by Ballerina!!! Adiós!! 🙋🏽

References

--

--

Chamindu Udakara

Technical Lead, Full-stack Developer, Baseball player, Tech Enthusiast