This scenario describes a three-component Job that retrieves user information based on the user ID from a REST service via HTTP GET and displays the retreived user information, as well as the message exchange between the client and the server, on the Run console.
If you are a Talend ESB user, create a Job
as described in Scenario 2: Using URI Query parameters to explore the data of a database, run
the Job to expose a REST service, and enter the REST service URL in your Web browser,
http://localhost:8088/users in this example. You should see information
like the following:
If you are not a Talend ESB user, then you need to get from your REST service provider the URL, the data structure, and the required parameters of the REST service you are going to call and make necessary modifications in the scenario configurations accordingly.
Drop the following components from the Palette onto the design workspace:
tRESTClient, used to call the REST service and retrieve user information from the server end,
tXMLMap, used to adapt the tree structure of the REST service, and
tLogRow, to display the retrieved user information on the Run console.
Connect the tRESTClient to the tXMLMap using a Row > Response connection.
Connect the tXMLMap to the tLogRow using a Row > Main connection, and give it a name, out in this example.
Label the components to best describe the actions to perform.
Configuring the service call
Double-click the tRESTClient component to open its Basic settings view.
Fill the URL field with the URL of the REST service you are going to invoke, "http://localhost:8088/users" in this example. Note that the URL provided in this use case is for demonstration purposes only and not a live address.
From the HTTP Method list, select GET to send an HTTP request for retrieving the existing records.
From the Accept Type list, select the type the client end is prepared to accept for the response from the server end, XML. Leave the rest of the settings as they are.
Click the [+] button beneath the Query parameters table to add two parameters, from and to, and set both parameters to 2, to get the information of the user with the ID of 2.
Alternatively, you can query the information of the user with the ID of 2 by adding ?from=2&to=2 to the service URL.
In the Advanced settings view of the tRESTClient component, select the Log messages and the Convert Response To DOM Document check boxes to log the message exchange to the server and convert the response from the server to document type.
Mapping the service structure and displaying the retrieved user information
Double-click the tXMLMap component to open the Map Editor.
If you selected XML in the Accept Type list of the tRESTClient component, define the input XML tree structure according to the service structure.
In the input table, right-click the default root node of the body column, select Rename from the contextual menu, and rename it to users.
Right-click the users node, select Create Sub-Element from the contextual menu, and create sub-element named user. Set user as the loop element because the XML structure of the Web service to be invoked is looped on this element.
Right-click the user node, select Create Attribute from the contextual menu, and enter id in the [Create New Attribute] dialog box to create an attribute named id for the user node.
Right-click the user node again, select Create Sub-Element from the contextual menu, and enter first_name in the [Create New Element] dialog box to create an sub-element named first_name for the user node.
Repeat this operation to create another sub-element under the user node, last_name.
Drop the id, first_name and last_name columns from the input table to the output table, and then click OK to validate the mapping and close the Map Editor.
If you selected JSON in the Accept Type list of the tRESTClient component, the response from the server end will be sent back in JSON format and converted to document type. In this example, the converted response structure looks like the following:
<root> <users> <user> <id>2</id> <first_name>Theodore</first_name> <last_name>Harding</last_name> </user> </users> </root>
Note that the
<root>element is removed if the Wrap JSON Response check box is cleared in the Advanced settings of the tRESTClient component.
Define the input XML tree structure accordingly and map it with the output data flow in a similar manner as described above.
Double-click the tLogRow component to open its Basic settings view.
Click the Sync columns button to make sure the component schema is synchronized with the output schema of the tXMLMap component.
In the Mode field, select the Table option to display the GET result in table cells.
Press Ctrl+S to save your Job.
Press F6 or click Run on the Run console to launch the Job.
The console shows that the tRESTClient component successfully reads the user information from the server end corresponding to the specified user ID.
If you selected XML in the Accept Type list of the tRESTClient component, the execution result will be:
If you selected JSON in the Accept Type list of the tRESTClient component, the execution result will be: