Building a CRUD RESTful API with WSO2 Micro Integrator

Arunan Sugunakumar
Think Integration
Published in
5 min readApr 30, 2021

--

With WSO2 Micro Integrator Data Services, users can integrate with different Data Sources and decouple the data from its infrastructure. In other words, when you create a data service in WSO2 Micro Integrator, the data that is stored in a storage system (such as the RDBMS) can be exposed in the form of a service. This allows users (that may be any application or system) to access the data without interacting with the original source of the data.

In this blog, we are going to create a CRUD RESTful API with Data Services in WSO2 Micro Integrator. We’ll create a table called City in the database, and develop a data service so that we can add and retrieve cities.

Creating a database

We’ll use MySQL as our database. Let’s use a mysql docker container. (If you have your own database that would be fine too.)

docker run --name mysql_db -p 3308:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.23

Go inside the docker container and create the database

docker exec -i -t mysql_db /bin/bash

Login into database. (Enter password as root)

mysql -u root -p

Create database.

create database city_db character set latin1;

Run the following script.

use city_dbCREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`city` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
);

Now that we have created the database, let’s create the DataService using Integration Studio.

I will be using Integration Studio 8.0.0 for demonstration purposes. It contains an embedded Micro Integrator 4.0.0. To download the Integration Studio, please visit https://wso2.com/integration/integration-studio/. Micro Integrator is the Integration runtime of API Manager 4.0.0

Creating Data Service using Integration Studio

  1. Open Integration Studio and create an Integration Project.

2. Inside the Integration Project, create a Data Service Configs project.

3. Inside the newly created Data Service Config project, create a Data Service.

4. Inside the Data Service, first we need to create a Datasource which is our MySQL database.

5. Next we need to create the queries against the datasource. Here I am going to create 3 queries : for reading the city list, adding a new city and deleting a city.

Let’s create the query for reading the list.

Next, we’ll create the query for adding a new city.

In the above query, we need to specify an input mapping since we need to pass the City name.

Similarly, create a query for deleting a city too.

You can see that we have created three queries now.

6. Next we need to create resources, which will execute these queries.

Create three resources, which will execute the three queries that we created above.

That’s it. You have created a Data Service for Micro Integrator successfully. If you check the source view of the Data Service, it should be like below.

Deploying and invoking the service

In the Composite Module, select the Data Service and export the CApp into the Micro Integrator.

Download the mysql-connector-java-8.0.19.jar and add to the Micro Integrator lib folder.

Start the Micro Integrator instance and make sure that the Data Service is deployed successfully.

Once started, you can invoke the following curl command and add a city to the database.

curl --location --request POST 'http://localhost:8290/services/CityDataService/cities' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"_postcities": {
"city" : "New York"
}
}'

The following curl command will retrieve the list of cities.

curl --location --request GET 'http://localhost:8290/services/CityDataService/cities' \
--header 'Accept: application/json'

--

--

Arunan Sugunakumar
Think Integration

Software Engineer (Integration) @wso2, Graduated from University of Moratuwa (Computer Science & Eng) and GSOCer @intermineorg