Calculate my BMI using WSO2 Datamapper mediator

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

Image for post
Image for post

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.

Prerequisites

You need to install the WSO2 EI Tooling to use the Data Mapper mediator.

Development Guidelines

  1. Open the WSO2 Developer Studio.
  2. Create the ESB Solution Project. Provide the name as BmiMeter.
Image for post
Image for post
Selecting ESB Solution Project

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

Image for post
Image for post
Creating REST API

4. Drag and drop a Data Mapper mediator and a Respond mediator as shown in the below picture.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post
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. Click on BmiMeterCompositeApplication project pom file.
  2. Select BmiMeter project and BmiMeterRegistry project.
Image for post
Image for post

3. Click on developer Studio -> Open Dashboard. Click on Servers tab

4. Click to create a new Server.

Image for post
Image for post

5. Click on Enterprise Integrator 6.4. Select Carbon Home and JDK if asks.

Image for post
Image for post

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.

Image for post
Image for post

Calculating BMI using the bmiapi.

  1. Open Postman
  2. Send the request as below.
height = 1.88 meters
weight = 72 kilograms
Image for post
Image for post

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

Image for post
Image for post

Written by

Senior Software Engineer — QA at WSO2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store