How to create a REST service Learn how to create a REST service provider with tRESTRequest and tRESTResponse components

In this tutorial, you will see how to build your first simple REST data service in both Talend Open Studio for ESB and Talend Enterprise ESB Studio by using Talend ESB REST components and the new tXMLMap component.

With this REST service, we will be able to explore the data of a simple database table containing information about customers. To do so, we need to create a service provider job.

To follow this tutorial, you need to download the file available at the bottom of this page, in the Download it! section of this tutorial.
The zip file contains a Job to import in the Studio to create the customer table used in this tutorial.

To import the prerequisite Job, click the Import items button from the Studio, select the in the Select archive file field and select the REST_prerequisite_0.1 Job.

Do not forget to update the connection properties of the tMysqlOutput component of the REST_prerequisite_0.1 Job.

Download it!

You want to practice?

Download to get the files used for this tutorial.

You can also download containing all the jobs needed to carry out this tutorial.

Next Step: [How to configure a REST application to return HRef links]


Create a provider data service Job Design

In the Repository to the left of the Studio main screen:

Right-click Job Designs.

In the menu, click Create Job to open the New Job wizard.

In the New Job wizard:

In the Name field, name the job RESTService.

Click Finish to close the wizard and create the job.

The Designer opens an empty job.


In the Name field, accents, special characters and spaces are invalid. Also, do not use numbers to start the field.

In the Palette to the right:

To add the REST service components, search "REST" in the Find component... field and press Enter.
The tRESTRequest and tRESTResponse components needed to create the service provider are displayed under the ESB > REST folder.

Click the tRESTRequest component and drop it, to the left of the canvas.

On the same folder, click the tRESTResponse component and drop it onto the Designer, to the right of the canvas.

In the Palette:

The same way, search for the tMysqlInput component and drop it onto the Designer between the two REST components.

This component will connect to the database we will explore with our REST service.

Now search for tXMLMap.
tXMLMap belongs to two different families.

Click one or the other tXMLMap component and drop it between the tMysqlInput and tRESTResponse components, as it will be used to transform the flat data of the database into XML data, as it is expected by a REST service.

And finally, search for tFlowToIterate, to help us connect the tRESTRequest component to the SQL data, and drop it between the tRESTRequest and the tMysqlInput components.

Configure the components and link them together

On the Designer:

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

In the Component view:

In the DB Version list, select the version of your DBMS.
In the Host, Port, Database, Username and Password fields, fill in the connection information to the database storing the sample data you imported (as required in the Prerequisite section at the beginning of this tutorial).

In the Table Name field, type in the name of the sample table: customers.

In the Query field, update to query to retrieve all the data from the customers table. To do so, type in "select * from customers".

And click the [...] button next to the Edit schema option to define the schema of the component.

In the Schema of tMysqlInput wizard:

Click the [+] button three times to add three new columns to the schema.

-Rename the first one id, select the Key check box and change its Type to Integer.
-Rename the second one firstname and leave the other options as they are.
-Rename the third column lastname and leave the other options as well.

Click OK to validate.

On the Designer:

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

In the Component view:

Click once 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, type in the XPath to the root of the XML tree: "/customers".
Click in the Output Flow field to display the [...] button and click it.

A Give the name for the schema wizard displays.

In the Schema Name field, name it getCustomers

Click OK.

A new wizard displays in which you can define the column of the schema you are about to create.

Here, we do not want to define any schema for this REST API Mapping for the time being, so simply click OK.


The new getCustomers mapping displays in the REST API Mapping table.

In the REST Endpoint field, you can type in the URI of the endpoint from which the service will be available. The URI is made of a Host name or IP address and a port number, separated by a colon. Here, we leave it as it is.

On the Designer:

To link the components together:
-Right-click tRESTRequest, select Row > getCustomers in the menu and click the tFlowToIterate component.
-Right-click tFlowToIterate, select Row > Iterate in the menu and click the tMysqlInput component.
-Right-click tMysqlInput, hold and drag to tXMLMap.
-Right-click tXMLMap, hold and drag to tRESTResponse and name the output link all when asked by the tXMLMap Output pop-up.


On the Designer:

Double-click tXMLMap to open its editor.

In the Editor:

The input flow from the database table displays to the left, in the input area of the editor. And in the output area to the right of the editor, configure the output XML flow that will be sent to the tRESTResponse component.

So, in the all output table, right-click the root element and select Rename in the menu to rename it customers.

Now let's define the XML tree of the customers root element.

Right-click customers and select Create Sub-Element in the menu to create a sub-element called customer that will hold each customer's information.

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

This way, a loop will be executed at each new customer encountered, to retrieve his individual information.

Click the Wrench button at the top of the table to display the output options and select true in the All in one list.

When the All in one option is set to true, all XML data is outputted in one single flow.

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

To do so, select the id column from the row1 table and drop it onto the customer sub-element of the all table, a Selection wizard displays.

In the wizard:

Select Create as attribute of target node.

Click OK to validate.

A mapping has been created between the id column of the customers database table and the id attribute of the customer XML tree.

Now, select the firstname and lastname column of the row1 table and drop them onto the customer sub-element of the all output table.

In the Selection wizard:

Select Create as sub-element of target node.

Click OK to validate.

The mapping between the input and output flow is complete.

Click Apply and OK to close the editor and validate the configuration.

Execute the 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.

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

If you have a look at your customers table, you can see that the data published as a service is the same: information about the customers with ids, firstnames and lastnames.


Download it!
Send it!
Share it!

[ top ]