Wednesday, 18 April 2018

Create a REST API in IBM Integration Bus from scratch

File ->New ->REST API

Enter a name for the REST API. The name that you specify is used as the name of the project in the IBM Integration Toolkit.




Select Create a REST API and define resources and operations yourself.

To finish creating the REST API, click Finish. The REST API Editor for the new REST API opens automatically.

To define resources, models, and operations in a REST API, complete the following steps:

1. Header Section
Use the Header section of the REST API Editor to display and modify general information about the REST API.
    The REST API base URL field displays the current base path for the REST API. All resources in a REST API are defined relative to its base path.
      The Title field displays the current title of the REST API. The Title is distinct from the REST API project name, and is defined as a property in the Swagger document.
        The Version field displays the current version of the REST API. It is defined as a property in the Swagger document.
          The header section also includes an example REST API base URL. The <hostname> and <port number> portions of this URL must be replaced with the hostname and port number of the integration server that the REST API is deployed to.


          2. Resources Section

          Use this button to create a new resource in the REST API. Select the operations to add. 



          After clicking OK, the follwowing is displayed.


          Use the Add button REST API add parameter and the Delete button REST API delete parameter to add or delete Header and Query parameters, and then set their Name, Type, and Data Type, and optionally, the Format, Required, and Description values.

          3. Model Definitions
          Use the Model Definitions section of the editor to define the JSON data that you can use for the Request and Response Schema type in  the postBusinessPartner operation.
          To create a new Model data type, click the Add button next to the Model Definitions field, and enter a name for your new JSON data type:
          Click on  <Enter q unique name to create a new model > and enter.

          To create an object Model when you have entered its name, use the Add a child for selection button REST API add parameter, located in the top right of the Model Definitions section, to add the first child. You can then add additional children to build up the required structure.

            (Image taken from IBM Knowledge Centre)

          4. Implementing an operation in a REST API

          Operations in a REST API are implemented as a subflow.
          To create a subflow for postBusinessPartner operation click.

          The subflow for the operation is created and is automatically wired in the main flow.

          In the subflow add a mapping node.


          Select 'Simple message map called by a message flow node' and click Next.


          In the next screen select your input and output maps.

          Implementing an error handler in a REST API

          I implemented a Catch Handler to catch all errors that may occur.

          A new subflow is automatically created and opened. Implement the error handler by adding any of the standard message flow nodes that are available in IBM Integration Bus to the subflow.

          Permitting web browsers to access a REST API by using Cross-Origin Resource Sharing(CORS)

          To permit web pages that are running in a web browser to make requests to a REST API that is running in IBM® Integration Bus Cross-Origin Resource Sharing (CORS) needs to be enabled.

          To enable CORS on the integration server HTTP listener run the following command.

          mqsichangeproperties integrationNodeName -e integrationServerName -o connectorName -n corsEnabled -v true

          e.g mqsichangeproperties IBNODE -e default -o HTTPConnector -n corsEnabled -v true

          To enable CORS on theintegration node HTTP listener run the following command.

          mqsichangeproperties integrationNodeName -b httplistener -o connectorName -n corsEnabled -v true

          e.g. mqsichangeproperties IBNODE -o HTTPConnector -n corsEnabled -v true





          3 comments:

          1. Hello, Could you please share the steps for adding the catch error handler also in the flow.

            ReplyDelete
            Replies
            1. Sorry for the late reply, Nidhi

              Please take a look at this IBM Knowledge Centre article

              https://www.ibm.com/support/knowledgecenter/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bi12028_.html

              Delete
          2. How can I add http header parameters in the request like content type,authentication....?

            ReplyDelete