16L02 IIB10006 REST API HRService From Swagger - Json
16L02 IIB10006 REST API HRService From Swagger - Json
Featuring:
September 2016
Hands-on lab built at product
version 10.0.0.6
IBM Integration Bus V10 Workshop September 2016
1.1 INTRODUCTION......................................................................................................................... 3
1.2 OPEN THE W INDOWS LOG MONITOR FOR IIB ............................................................................. 3
1.3 CONFIGURE TESTNODE_IIBUSER FOR REST APIS ................................................................. 5
1.4 CONFIGURE INTEGRATION BUS NODE TO WORK WITH DB2 ......................................................... 6
4. APPENDIX ................................................................................................................................... 39
From the Start menu, click IIB Event Log Monitor. The Monitor will open; it is useful to have this
always open in the background.
This tool is not shipped as part of the IIB product; please contact us directly if you would like a
copy.
This lab, version 10.0.0.3, has been updated significantly from earlier versions. The
following changes have been made:
You should use the Windows user "iibuser". This user is a member of mqbrkrs and
mqm, but is not a member of Administrators. The user "iibuser" can create new IIB
nodes and do all required IIB development work. However, installation of the IIB
product requires Administrator privileges (not required in this lab).
The database has been changed from the DB2 SAMPLE database to the DB2 HRDB
database. HRDB contains two tables, EMPLOYEE and DEPARTMENT. These tables
have been populated with data required for this lab. (The DDL for the HRDB is available
in the student10 folder; we intend to provide corresponding DDL for Microsoft
SQL/Server and Oracle over time).
The map node now retrieves multiple rows from the database, using an SQL "LIKE"
function . Additionally, the map has been refactored to use a main map and a submap.
Both the main map and submap are located in a shared library.
Input to the integration service and the REST service is now a simple schema
containing just one element, the required employee number.
As a consequence, this version of the lab, and the associated solution, can only be
used with the corresponding changes in other labs. Use version 10.0.0.3 of all labs in
this series of lab guides.
IIB v10, fixpack 5 (10.0.0.5) has made significant changes in the Toolkit representation
of REST APIs, the editor functions, and with the tools provided with the Mapping Node
editor. This lab has been updated to reflect those changes.
This lab now uses JSON models throughout, and has removed the parts of the lab that
were based on earlier XML schema components. The Mapping nodes use the Map Node
wizard, which automatically creates input and output assemblies, based on the JSON
model definitions.
Use of the pre-built submap (used in earlier versions) has been removed, and replaced
with a single mapping node.
The instructions in this lab guide are based on a Windows implementation, with a user
named “iibuser”.
The Windows VMWare image on which this lab is based is not available outside IBM, so you
will need to provide your own software product installations where necessary.
Login to Windows as the user "iibuser", password = "passw0rd". (You may already be logged
in).
The IIB support for the REST API requires some special configuration for the IIB node and server.
2. Enable Cross-Origin Resource Scripting for REST. This is required when testing with the
SwaggerUI test tool. See
http://www.w3.org/TR/cors/?cm_mc_uid=09173639950214518562833&cm_mc_sid_502000
00=1452177651 for further information.
(Helpful hint - the VM keyboard is set to UK English. If you cannot find the "\" with your
keyboard, use "cd .." to move the a higher-level folder in a DOS window), or change the
keyboard settings to reflect your locale.)
In an IIB Integration Console (shortcut on the Start menu), run the following command.
Note, the text should be typed on a single line - the parameters are shown on different lines
here for readability; the same approach is taken throughout this and other lab documents.
mqsichangeproperties TESTNODE_iibuser
-e default
-o HTTPConnector
-n corsEnabled -v true
To run this lab, the Integration Bus node must be enabled to allow a JDBC connection to the
HRDB database.
c:\student10\Create_HR_database
Accept the defaults presented in the script. This will create the required JDBC
configurable service for the HRDB database.
4. Stop and restart the node to enable the above definitions to be activated
mqsistop TESTNODE_iibuser
mqsistart TESTNODE_iibuser
This will create the necessary security credentials enabling TESTNODE_iibuser to connect to the
database.
c:\student10\REST_API_HR_Service\resources\
HR_Employee_and_Department_Services.json
Open the file with the Notepad++ editor (right-click, select Edit with Notepad++).
We have installed a JSON document plugin into Notepad++, so this JSON document will be
formatted for easy reading.
The JSON document has been constructed to define interfaces for the EMPLOYEE and
DEPARTMENT tables. The document also has definitions for DBResp (for database
response information, EmployeeResponse (includes DBResp and EMPLOYEE), and
DepartmentResponse (includes DBResp and DEPARTMENT).
The main section of the document is a series of operations (GET, POST, PUT, etc),
associated with different types of operation (getEmployee, listEmployees, etc).
Near the bottom of the document are two definitions for EMPLOYEE and DEPARTMENT,
which defines the precise structure of these elements.
This document will be used as the basis of the REST API that you will build in IIB.
Look for the variable "EMPNO", which is referenced in several places in the document. This
variable will be used by the IIB REST API.
The input parameter to the operation that you will implement is “employeeNumber”; this is
defined as a path parameter for the required operation. Note the “required” parameter is set
to “true”.
The HRDB shared library and associated project will not be developed in this lab - see Lab 01 from
the 2015 series on lab guides (Creating an Integration Service) for details of how to do this. In this lab,
you will import a pre-built version of this project.
1. Start the IIB Integration Toolkit, and create a new workspace. If you already have a
workspace open, click File, Switch Workspace. Give the new workspace the name
c:\users\iibuser\IBM\IIB 10\workspace_REST
C:\student10\REST_API_HR_Service\resources\HRDB.zip
Import both the HRDB shared library and HRDB_project from this PI file, and click Finish.
3. When imported, you will see the HRDB shared library as shown here. Note that
HRDB_project is an Independent Resource (ie. it is a plain project, not a shared library).
Click Next.
c:\student10\REST_API_HR_Service\resources\
HR_Employee_and_Department_Services.json
You will see warnings about unused definitions. You can ignore these (they will be
implemented later).
However, in this lab, you will use the Integration Toolkit editor, so switch back to the Toolkit
and click Next.
5. The summary window will show you all of the REST operations that were defined in the
JSON document. These operations were constructed to match the EMPLOYEE and
DEPARTMENT tables in the HRDB database.
Note there is an operation named getEmployees (ie. retrieve a list of all employees), and an
operation named getEmployee. This lab will implement the getEmployee operation.
Click Finish.
6. The swagger document has now been imported into the Integration Toolkit. The import
process has also created a base REST API and a message flow that implements the REST
API.
The imported and generated items are split into five main sections in the REST API editor:
Header - containing the base URL for the REST API, title and description
Resources - containing all the resources from the swagger document, and all of the
operations that are contained within each resource
Model Definitions - schema definitions for the input and output JSON objects
Error Handling - options to add some elements of runtime security
Security - basic security parameters
For some of the operations (for example, the updateEmployee PUT operation in this
resource), the Schema type has been set (in this case to EMPLOYEE).
For some other operations (for example the getEmployee GET operation), the input
parameter is specified (employeeNumber). Because {employeeNumber} has been specified
as part of the REST API URL path, the input parameter name (employeeNumber) is not
editable.
The Schema Type of the successful (200) operation has been set to EmployeeResponse
(originally specified in the swagger doc).
You can use the Schema Type dropdowns to change the required schema for the operation.
The available values are derived from the Model Definitions section. If no schema type is
specified, the REST operation can dynamically specify the format of the output message.
8. Before proceeding with the implementation, the REST API project has to reference the
HRDB shared library. This is because you will use a map to retrieve data from the database;
the map references elements in the HRDB.dbm file.
Expand the REST API Resources, and position the editor at the
/employees/{employeeNumber} resource.
2. In the top right part of the description, click on the icon to "Create a subflow for the
operation".
This will create a skeleton subflow where you will provide the logic to implement the
operation:
3. Drop a Mapping node onto the flow editor as shown. As soon as you drop the node onto the
flow editor, change the name of the node to getEmployee, and click Return (this should
automatically name the map file, as shown in the node properties, below. The map name is
formed by concatenating the REST operation name (getEmployee in this example) with the
label of the mapping node (also getEmployee in this example).
Note that the new Map wizard has provided two options:
Click Finish (Next is not available as an option, since the map wizard automatically creates
the inputs and outputs).
2. The basic map is shown. Because the getEmployee operation is a GET method, the input is
defined as a BLOB and the output message is a JSON message. The input message can be
defined as a BLOB because for a GET operation, the input parameters are provided in the
message header, and it is not necessary to parse the message body.
3. Note that the input message assembly is already shown with the LocalEnvironment folder.
This is because IIB places some parts of the input of a REST API into the LocalEnvironment,
and the Mapping Node will almost always need access to this for developing application
logic.
Expand the LocalEnvironment folder, and fully expand the REST subfolder.
Note that employeeNumber has already been added to the REST folder - this was derived
from the input Schema Type on the swagger definition of the getEmployee operation.
Also note that IIB 10.0.0.6 has added the Response folder under the REST folder.
4. Now focus on the output message assembly. The Map wizard has provided a JSON output
assembly (because the getEmployee operation specified a schema type of
EmployeeResponse for the response message).
Fully expand the output assembly. The JSON Data is of type EmployeeResponse.
You will see the DBResp (JSON object) and Employee (JSON array) items underneath the
Data element.
Ensure the Data (type EmployeeResponse) element is highlighted. This is important for the
subsequent step which will automatically create a Select transform, and connect it to the
highlighted element.
5. Now add a transform to retrieve data from the HRDB database. Click the "Select rows from a
database" icon at the top of the map editor.
6. In the New Database Select window, the HRDB database should already be available and
selected (because you set the HRDB Library reference).
A new XPath expression will have been created so that the SQL clause can reference the
required input element from the LocalEnvironment (employeeNumber).
The map will be built to retrieve all employee records that partially match the provided key.
For example, if the provided key is "0020", we want the map to retrieve rows with a value of
"000020", "00201", "00204", etc.
This is done by extending the SQL LIKE statement, in conjunction with the "%" character,
appended both as a prefix and a suffix. Hence the SQL statement "SELECT xxxx
LIKE %0020%" will achieve the result described above.
Now double-check that you made the XPath changes exactly as shown above !!
10. A Select transform will have been placed into the map, connecting to the output “Data”
element (of type EmployeeResponse). If you didn’t previously highlight the Data element, the
connection will be in the wrong place; you can adjust the connector by highlighting it, and
dragging to the desired target.
Click the Select transform (click the Select text, not the surrounding transform icon). This is
where you will define the precise element mappings that you need. Clicking Select will take
the editor to the next lower logical level of the map (synonymous with a “Do … End”
programming construct).
11. Expand the output Employee element to expose the Item element.
Connect ResultSet Row to the Item array element in the output Data. This will generate a
"For each" transform; this means that the transform will be performed for each row returned
from the database. However, you still haven’t defined the individual element mappings – see
the next step.
12. Click the text "For each" to specify the individual element mappings. This will take you to the
next lower logical level of the map.
Click the Automap icon to invoke the wizard to create the mappings automatically.
14. Return to the higher level of the map by clicking the yellow up arrow.
Expand DBResp, and connect the input ResultSet to the output DBResp/RowsRetrieved.
Again, this will initially create a "For each" transform; the transform will show a warning.
16. Click the drop-down arrow on the transform, and change the transform to "Custom XPath".
17. In the map editor, make sure the Custom XPath transform is selected.
On the Properties tab for this transform, select "General" and type the following into the
XPath editor:
fn:count(
Then, invoke the content assist function (Ctrl-space) which will show you the possible values
for completion. Select the value that shows $ResultSet, or similar. Depending on whether
you have made other editing changes, the value shown may be $ResultSet or have a
suffixed number. The example shown below is $ResultSet.
Complete the XPath expression with a ")". This expression will calculate the number of rows
retrieved from the database.
Expand the output assembly DBResp, and right-click UserReturnCode. Select Add Assign.
19. The default value for an Assign is "0", so leave this unchanged.
20. Finally, go up one level in the map (yellow up arrow), and delete the Task transform that was
added by the Map Wizard.
The main map is now complete, so save (Ctrl-S) and close the map.
2. You will be switched to the default browser. Fully expand TESTNODE_iibuser, down to the
HR_Service REST API, as shown below.
Under HR_Service, click "API", which will show you the available operations in the REST
API, and whether they have been implemented. Check that you have implemented the
correct operation – it should be getEmployee.
It will also show you the URLs for the REST API and the definitions (the .json file).
3. On the "REST API Definitions URL", right-click and select "Copy Link Location".
COPY
4. In Firefox, open a new tab, and open the SwaggerUI tool (using the bookmark in the REST
folder).
5. In the entry field (not the browser address field), paste the contents of the clipboard and click
Explore.
PASTE
6. We are concerned with the getEmployee operation so click "List Operations" to show the
operations related to employees.
Note that SwaggerUI does not have any knowledge at this point of whether the operation has
been implemented.
If successful, the returned data will look something like this. Note the database response
information (user return code, number of rows returned), as well as the user data.
Note that you can copy the Request URL below, and paste directly into a browser.
9. Provide an employeeNumber that does not exist, for example 000012 (but make sure you
use an employeeNumber that has 6 characters).
You will see the service has worked (UserReturnCode = 0), but no data has been found
(RowsRetrieved = 0).
10. Provide an employeeNumber that will generate several matches, for example "0020". This
will use the "LIKE" operator in the SQL where clause.
Two records will be retrieved from the database. You will see the element UserReturnCode =
0, and the element RowsRetrieved = 2.
Use the slide bar to see the data of the two returned rows.
4. Appendix
If used in conjunction with the workshop VMWare image, these commands must be run under the
user "iibadmin". Appropriate database permissions are included in the scripts to GRANT access to the
user iibuser.
When an IIB node is created, or recreated, the appropriate JDBC and security definitions must be
created. Run the command files 3_Create_JDBC_for_HRDB and 4_Create_HRDB_SecurityId.cmd.