How to add a new REST API Mapping to an existing REST service Learn how modify and add a new REST API Mapping to a REST service

In this tutorial, we will see how to handle the URIs that are passed as attributes to our customer XML schema by creating a new REST API Mapping to further explore the data of the database and modifying an existing one to dynamically explore the data of the database.

Prerequisites:
To follow this tutorial, you should have executed tutorials [How to create a REST service] and [How to configure a REST application to return HRef links].

Download it!

Do you want to practice?

Download tutorialProject.zip includes all the jobs needed to carry out this tutorial. Please, download it!

You can also:
Send it!

Share it!

 


Add a new REST API Mapping


In the Designer:

Double-click the tRESTRequest component to display its corresponding Component view and set its properties.

In the Component view:

Click once on the [+] button at the bottom of the REST API Mapping table to add a new mapping.
In the HTTP Verb field, leave the GET operation.
In the URI Pattern field, leave the field as is for the moment.
Click in the Output Flow field to display the [...] button and click it.

Next
The Give the name for the schema dialog box displays.

In the Schema Name field, name it getCustomer.

Click OK.

Next
In the schema wizard:

Click the [+] button once to add a new line to the table.

Rename it id and change its Type to Integer.

Click OK to validate.

Next
The new getCustomer mapping displays in the REST API Mapping table.

In the URI Pattern field, type in the XPath to the root of the XML tree followed by the id variable to each customer: "/customers"/{id}".

Next
In the Designer:

Duplicate the tMysqlInput and tRESTResponse component simply by copy-pasting them.

And place them in the canvas as displayed in the screen capture.

Next
In the Palette to the right:

Search for tXMLMap in the Find component... field and press Enter.
tXMLMap belongs to two different families, Processing and XML.

Click one or the other tXMLMap component and drop it between the two tMysqlInput components.

Next
On the Designer:

To link the components together:

-Right-click tRESTRequest, select Row > getCustomer in the menu and click the second tXMLMap component.
-Right-click the second tMysqlInput component, hold and drag to tXMLMap.
-Right-click the tXMLMap component, hold and drag to the second tRESTResponse component and name the output link single when asked by the tXMLMap Output pop-up.

Next
On the Designer:

Click tXMLMap to select it and click the Component view at the bottom of the Studio to display its properties.

Click [...] next to the Map Editor field to open the component's editor.

Note:
you can also simply double-click tXMLMap on the Designer and its editor will open.

Next
In the tXMLMap editor:

Select the id column of the getCustomer input table and drag it to the id column of the row2 table.
A purple arrow is created between the columns of the tables to represent the join.

Next
Now we will define the XML schema of the tRESTResponse component.
To do so:

Right-click the root element node in the single table and select Rename in the menu to rename it customer.

Next

Right-click this new customer element node and select As loop element in the menu.

When executed the loop will retrieve the customer's details for each Customer Id found.

Next
Now let's do the mapping between the input and the output flow.

To do so, select the id column from the row2 table and drop it onto the customer element of the single table, a Selection wizard displays.

In the wizard:

Select Create as attribute of target node.

Click OK to validate.

Next
Now, select the firstname and lastname column of the row2 table and drop them onto the customer element of the single output table.

In the Selection wizard:


Select Create as sub-element of target node.

Click OK to validate.

Next
The mapping between the input and output flow is complete.

Now, let's add a new attribute. To do so:

Right-click the customer element node and select Create Attribute in the menu.

Next
In the Create New Element wizard:

Name the new attribute to be created all.

Click OK to validate.

Next
In the Var table in the middle of the editor:

Click the [+] button to add a new variable to the table. This variable is automatically called Var1.

Click the Expression field of this Var1 variable to display the [...] button.

Click the [...] button to open the Expression Builder editor and build the variable.

Next
In the Expression Builder editor:

Press Ctrl+Space in the Expression field to display the auto-completion menu.
In the menu, scroll to the tRESTRequest_1.URI_BASE variable and double-click it to select it.

Next
The selected variable displays in the Expression field, customize it by adding the following code: +"customers".

Click OK to validate.

Next
Once created, select the Var1 variable from the Var table and drag and drop it onto the @all field of the single output table.

Click Apply and OK to validate.

Next
Execute the updated provider data service Job Design


In the Designer:

Press F6 to run the Job.
The Job is automatically saved before being executed.

The Run view displays at the bottom of the Studio and the console follows the execution. We can see that the REST Service has been published to the following endpoint: http://localhost:8088/.

Note: the provider continuously listens for request, so if you want to stop it, you will have to click the Kill button of the Run view.

Next
To check the service, simply go to the endpoint address on which it has been published: http://localhost:8088/customers.

We can see that it looks like in the previous tutorial.

Next
Now if you select and copy the href value of the first customer and paste it in your browser, you can see the XML data corresponding to the second get method we created with two attributes: the id of the customer and the all URI to the root of our service, and the firstname and lastname elements.

Next
Update the REST API Mapping to implement dynamic queries


Now, let's customize a bit more the first getCustomers mapping that we implemented. To do so, in the Designer:

Double-click the tRESTRequest component to display its corresponding Component view and set its properties.

In the Component view:

Click the [...] button of the getCustomers REST API Mapping.

Next
In the wizard:

Click the [+] button twice to add two columns to the schema.

-Rename the first one from, set its Default value to 1 and type in query as Comment.
-Rename the second column to  to, set its Default value to 15 and type in query as Comment.

Click OK to validate.

Next
Now let's modify the query of the tMysqlInput component to reflect and implement the update of the REST mapping. To do so:

Double-click the tMysqlInput component to displays its corresponding Component view and set its properties.

In the Component view:

In the Query field, update to query as follows: "select * from customers where id >= " + globalMap.get("getCustomers.from") + " and  id <= " + globalMap.get("getCustomers.to").

Next
In the Designer:

Press F6 to run the Job.
The Job is automatically saved before being executed.

The Run view displays at the bottom of the Studio and the console follows the execution. We can see that the REST Service has been published to the following endpoint: http://localhost:8088/.

Note: the provider continuously listens for request, so if you want to stop it, you will have to click the Kill button of the Run view.

Next
To check the service, simply go to the endpoint address on which it has been published: http://localhost:8088/customers.

Next
Now, type in the following URI in your Web browser: http://localhost:8088/customers?from=7

You will retrieve all of the customers from 7 onwards. Here, there are only 10 customers in our database, so, our getCustomers REST API Mapping helps us retrieve customers from 7 to 10.

Next
Now, type in the following URI in your Web browser: http://localhost:8088/customers?from=2&to=4

You will get all the customers from 2 to 4.


 

    Download it!     Send it!     Share it!

Do you want to practice?

Download tutorialProject.zip includes all the jobs needed to carry out this tutorial. Please, download it!

Friends / colleagues may be interested in this tutorial? Send it to them!

You liked this tutorial ? Support it!

[ top ]