WSO2 Data services: Automatic CRUD services

Daniel S. Blanco
3 min readMay 20, 2020

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:

  • The port for HTTP call is still 8280 and 8243 for HTTPS.
  • The context for invoking these services is odata and not services.
  • We still need to put the name of our data service. In this case, it is ODataSample.
  • After the name of the data service we must indicate the name of the data source we have configured. In this case ODataDS.
  • Finally we must indicate the name of the table. In our example, BOOK.

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

  • If we do not indicate any table. The call will show us the information of the different existing tables.
  • If we do not indicate any table, add to the path /$metadata and we invoke it as application/xml, it will return the format information of all the tables of the data source.
  • If we want to obtain only certain fields from a table we must add ?$select=fieldName, with the name of the fields to obtain separated by commas.

Example:

curl --location --request GET 'https://localhost:8243/odata/ODataSample/ODataDS/BOOK?$select=NAME,AUTHOR' \
--header 'Accept: application/json'
  • If we want to filter the table data we will add to the path ?$filter=fieldName eq ‘fieldValue’.

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"
}'

--

--