Calculate my BMI using WSO2 Datamapper mediator

Let’s create an API which calculates your BMI using WSO2 Datamapper mediator.

Datamapper mediator

Prerequisites

Development Guidelines

  1. Create the ESB Solution Project. Provide the name as BmiMeter.
Selecting ESB Solution Project

3. Right click on the project and create a REST API.

Creating 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.

input.xml

<?xml version=”1.0" encoding=”UTF-8"?>
<CalculateBmi>
<height>[height]</height>
<weight>[weight]</weight>
</CalculateBmi>

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.

output.xml

<?xml version="1.0" encoding="UTF-8"?>
<BmiResult>
<bmi>[bmi]</bmi>
</BmiResult>

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.

datamapper_diagram

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.

Deployment Guidelines

  1. 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.

  1. Send the request as below.
height = 1.88 meters
weight = 72 kilograms

You will get the response as below which is the calculated BMI

Senior Software Engineer — QA at WSO2