Let’s create an API which calculates your BMI using WSO2 Datamapper mediator.
Data Mapper mediator is a data mapping solution that can be integrated into a mediation sequence. It converts and transforms one data format to another, or changes the structure of the data in a message. It provides a WSO2 Developer Studio-based tool to create a graphical mapping configuration and generates the files required to execute this graphical mapping configuration by the WSO2 Data Mapper engine.
You need to install the WSO2 EI Tooling to use the Data Mapper mediator.
- Open the WSO2 Developer Studio.
- Create the ESB Solution Project. Provide the name as BmiMeter.
3. Right click on the project and create a REST API.
4. Drag and drop a Data Mapper mediator and a Respond mediator as shown in the below picture.
5. Click on the API Resource, and then click on its Properties tab, and select True as the value for the Post method as shown below, to create the API resource listening to POST requests.
6. Double click on the Data Mapping mediator to configure it. You view a dialog box to create a registry resource project.
7. Enter a name for the configuration, and point the Registry Resource project to save it.
8. Click OK. You view the Data Mapper diagram editor in the new WSO2 Data Mapper Graphical perspective.
9. Create an XML file by copying the following xml file content, and save it in your local file system.
<?xml version=”1.0" encoding=”UTF-8"?>
10. Right-click on the top title bar of the Input box and, click Load Input.
11. Select XML as the Resource Type.
12. Click the file system link in Select resource from, select the XML file you saved in your local file system in step 9, and click Open.
13. Create another XML file by copying the following sample and save it in your local file system.
<?xml version="1.0" encoding="UTF-8"?>
14. Right-click on the top title bar of the Output box and, click Load Output.
15. Click the file system link in Select resource from, select the XML file you saved in your local file system in step 13, and click Open.
16. Do the mapping using operators as shown in the example below.
BMI calculation is as below.
BMI = weight in Kg / (height in meters * height in meters)
Explanation of the diagram
First we get the weight from Kilograms and height from meters from the user. They are taken as strings. It is sent through StringtoNumber operators which will be converted to Numbers. Then the height will be multiplied by the same value. Then the weight will be divided by (height *height). It will output as the BMI value.
17. Press Ctrl+S keys in each tab, to save all the configurations.
- Click on BmiMeterCompositeApplication project pom file.
- Select BmiMeter project and BmiMeterRegistry project.
3. Click on developer Studio -> Open Dashboard. Click on Servers tab
4. Click to create a new Server.
5. Click on Enterprise Integrator 6.4. Select Carbon Home and JDK if asks.
6. Start the server. The carbon application and the API will be deployed.
7. Management Console will be automatically open in the Browser. Log in to it by providing credentials.
8. Click on Service Bus -> APIs. You can see the BmiApi has been deployed successfully.
Calculating BMI using the bmiapi.
- Open Postman
- Send the request as below.
height = 1.88 meters
weight = 72 kilograms
You will get the response as below which is the calculated BMI