Developing A REST API Service: IBM Integration Bus
Developing A REST API Service: IBM Integration Bus
Featuring:
June 2015
Hands-on lab built at product
Version 10.0.0.0
IBM Integration Bus V10 Workshop June 2015
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.
1. Support for REST requires the use of the embedded HTTP listeners, rather than the node-
wide listener.
Additionally, the IIB node has to be configured for Cross-Origin Resource Sharing (CORS).
For details on this, please read: http://www.w3.org/TR/cors/.
To configure these items, we have provided a script. In an IIB Command Console, change
directory to
c:\student10\REST_service\install
Accept the default values for the IIB node name (RESTNODE).
This script will create the new node, and run two key commands:
Enable HTTP embedded listeners. The REST support is only provided for embedded
listeners, not the node-wide listener:
mqsichangeproperties RESTNODE
-e default
-o ExecutionGroup
-n httpNodesUseEmbeddedListener -v true
Enable Cross-Origin Resource Scripting for REST. This is required when testing with
the SwaggerUI test tool. See http://en.wikipedia.org/wiki/Cross-
origin_resource_sharing for further information.
The script will also configure the JDBC parameters for connection to the SAMPLE database,
which will be used in this scenario.
When the node is fully restarted, you will see the following messages on the IIB Event Log
viewer:
BIP3132I: ( RESTNODE ) The HTTP Listener has started listening on port
''4418'' for ''WebAdmin http'' connections. [08/04/2015 09:16:21]
BIP2152I: ( RESTNODE.default ) Configuration message received from
integration node. [08/04/2015 09:16:22]
BIP2153I: ( RESTNODE.default ) About to ''Start'' an integration server.
[08/04/2015 09:16:22]
BIP2154I: ( RESTNODE.default ) Integration server finished with
Configuration message. [08/04/2015 09:16:22]
c:\student10\REST_service\resources\EmployeeService.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 main section of the document is a series of operations (GET, POST, PUT, etc),
associated with different types of operation (getEmployee, listEmployees, etc).
At the very 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 applications that you will build in IIB.
Look for the variable "employeeNumber", which is referenced in several places in the
document. This variable will be used by the IIB REST application.
C:\student10\integration_service\solution\EmployeeServiceInterface.V10.zip
When imported, you will see the Shared Library like this:
c:\student10\REST_service\resources\EmployeeService.json
Click Next.
4. 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 SAMPLE 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.
5. The operations have now been imported into the IIB Toolkit. The import process has also
created a base REST application and a message flow that implements the REST service.
Expand each operation. You will see that you can implement the operation by clicking the
"Implement the operation" link.
6. Before proceeding with the implementation, the REST service has to reference the required
Shared Library. This is because it needs access to both the schema definitions and the IIB
getEmp map that are stored in the library.
In the navigator, right-click the EmployeeService_REST service and select Manage Library
references.
In the Operations list, locate getEmployee and click "Implement the operation".
This will open the subflow editor. Each operation is implemented with a separate subflow.
Do not confuse this with the /getEmployees operation in the /employees section, just
above.
3. This mapping node will actually be the getEmp map that is already built and available in the
EmployeeServiceInterface, so you must change the properties of the node to reference this
map.
Highlight the map, and select the Properties of the Map node. Click Browse to select the
getEmp map from the shared library.
If you are using the prebuilt version of the Shared Library, you will also see a map called
updEmp.
Click OK.
4. REST GET operations will be contained within the HTTP header part of the incoming
message. When IIB receives such an incoming message, it extracts the REST parameters
and places them into the Local Environment. IIB v10 has introduced a new part of the
LocalEnv called REST.
The getEmp map requires its input in the form defined by the EMPLOYEE schema. To
construct the message in this format, you will develop a new mapping node to obtain the
incoming parameter for the getEmployee operation. The mapping node will use this
information to construct a message in the format required by getEmp.
Drop two further nodes onto the flow and connect as shown:
5. Open the extractKeyFromLocalEnv map, and click Next at the first window.
For the map inputs and outputs, make the following selections:
Input
o IBM supplied message models - BLOB (the subflow will obtain its
information from the LocalEnvironment, and there is no need to parse the
message payload)
Output
o EmployeeServiceInterface, XML schemas - EMPLOYEE
Click Finish.
7. For a REST GET operation, the employee key will be available in the LocalEnvironment. For
the map to access the Local Environment, you must explicitly add this header to the
Message Assembly.
On the input Message Assembly, right-click and select "Add or remove headers and folders".
8. Expand the Local Environment and the REST section (located near the bottom of the Local
Environment).
Incoming REST parameters will appear under the REST/Input/Parameters element, so the
definition of this element needs to be added here.
9. Name the new element employeeNumber. Note, this has to match the precise name and
case of the JSON employeeNumber element that you saw in the JSON document.
10. The new element needs to be mapped to the output EMPLOYEE/EMPNO message.
Unfortunately, expanding the Local Environment has probably meant that the output
message has disappeared from the map display. To handle this, perform a "Quick Link".
Right-click employeeNumber, and select Quick Link.
Collapse the Properties element, and in the EMPLOYEE element, select EMPNO.
12. A Move transform will be generated. If you wish, you can use the QuickFix to correct the
cardinality warning message (hover over the small light on the Move transform, then select
"Set cardinality to first index").
13. One final node is required. In the flow editor, drop a third map node onto the flow and name it
convertXMLToJSON. Connect as shown.
The map is required because the output needs to be sent back to the originating REST
request in JSON format. (The getEmp map produces an output message in XML format).
Click Next at the first window, and for the inputs and outputs, make the following selections:
Input
o EmployeeResponse
Output
o IBM supplied message models - JSON object
Click Finish.
15. The output JSON message needs to be Cast as EmployeeResponse. Fully expand the
JSON output, and right-click the final "any".
Select Cast.
Click OK.
2. Open the IIB web UI by right-clicking RESTNODE and selecting Start Web User Interface.
3. You will be switched to the default browser. Fully expand RESTNODE, down to the
EmployeeService_REST, as shown below.
Under EmployeeService_REST, click "API", which will show you the available operations in
IIB and whether they have been implemented. Check that you have implemented the correct
operation.
It will also show you the URLs for local and remote invocations, and the REST API
definitions (the .json file).
4. On the "Remote URL for REST API definitions", right-click and select "Copy Link Location".
5. In Firefox, open a new tab, and open the SwaggerUI tool (using the bookmark in the REST
folder).
6. In the entry field, paste the contents of the clipboard and click Return (or click Explore).
7. 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.
The employeeNumber will show the expression (required). Replace this with a suitable
value, say 000010.
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.
10. 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).
11. Now use an invalid length key, for example 0000102, which has 7 characters.
You will see that although the request completes, the UserReturnCode = -1. This indicates
that a database error has occurred, and the relevant SQL errors have been passed back to
the REST client. In this case, the SQLCODE of -302 indicates that the key field is too long