WSO2 Data services: Automatic CRUD services

We had already done a post about how to do REST data services with WSO2, here. Today we will see the OData services. Or what is the same, the automatic generation of REST data services. OData refers to Open Data Protocol, an OASIS standard. This standard defines the good practices to build and consume a RESTful API. It is something quite simple to do but I think it is a feature of WSO2 that not many people know.

To be able to automatically generate these web services, we must follow the next steps from the administration console:
- Create a new data service
- Associate a data source, with the difference that we must mark the option to enable OData.
- Save the data source and finish editing the data service.
If we see the code we can verify that it is a very simple data service:

Only with this WSO2, will generate a CRUD service for each table of the data source that we have indicated. This is an important point because if the data source contains system tables, the data service generation will fail. And if you want to create a CRUD for database views is only possible if we apply a WUM on the Enterprise Integrator.

Once generated, let’s see how we can get all the data from the BOOK table

curl --location --request GET 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK' \
--header 'Accept: application/json

If we see the previous call we must be aware of some details:

Also associated to GET method we can do the following operations:

Example:

curl --location --request GET 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK?$select=NAME,AUTHOR' \
--header 'Accept: application/json'

Example:

curl --location --request GET 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK?$filter=NAME%20eq%20%27Hyperion%27' \
--header 'Accept: application/json'

If we want to delete a record we will make the following call:

curl --location --request DELETE 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK(26)'

To create a record we must use the POST method as usual and indicate in the body of the call the fields to be entered.

curl --location --request POST 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"NAME":"Ender's game",
"AUTHOR": "Orson S. Card"
}

And finally, to make an update, we will use the PUT method.

curl --location --request PUT 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK(28)' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"NAME":"El juego de Ender"
}'