Search Results

Using Basic REST Binding

The Basic REST sample models a bookstore. This sample shows you how you can use a RESTful service to add and retrieve books from a book collection. This sample uses two REST methods: POST and GET. This sample has two applications, one containing the service process (Books.bwp) and the other containing the client process (Client.bwp) which is used to invoke the service.

You can start by examining the Books.bwp service process first.

Prerequisites

Install the latest version of the Google Chrome browser in the default location.

Service Application

The REST service application in this sample contains the books.bwp process. This service supports two operations, POST and GET. POST allows you to add one or more books to the book collection. GET allows you to retrieve all the books in the book collection. After you create a REST service, a .json Swagger 2.0 document gets generated in the Service Descriptors folder which can be used to create a REST Reference Binding to invoke the REST service. In this sample it creates the Books.json.

Note: If you are accessing the sample from the TIBCO Business Studio Welcome page, go directly to Step 3.

Viewing the Books.bwp REST service process

Open the Books.bwp process and view it in the Process Editor. To open the process, follow these steps:

  1. In File Explorer view, expand the samples > binding > rest > Basic folder and double-click tibco_bw_sample_binding_rest_basic_service.zip to expand the service project in the Project Explorer.
  2. In Project Explorer expand the tibco_bw_sample_binding_rest_basic_service project.
  3. Fully expand the Processes directory and double-click Books.bwp to open it in the Process Editor pane as shown below.

    The books icon on the left edge of the Process Editor represents the service and its operations. The cloud around the green chevron indicates that the service is a REST service.

    The POST operation is configured with the postOut activity. In the books/post operation, click postOut activity to see what the POST operation is configured to return when successful. In the Properties view, click the Input tab. As shown below, the POST operation is configured to return the definition of the books that were successfully posted:

    The GET (books/get) operation retrieves the books. It is configured with three activities, RenderJSON, ParseJSON, and getOut. The books data is hard-coded in the RenderJSON activity. This data is passed to the ParseJSON activity which parses the data and passes it on to the getOut activity which displays the response to the GET request.

Running the application

You have the option to run the process in Debug mode or regular mode. The Debug mode gives you detailed logs of the execution which can help you with debugging the runtime errors if need be. To run the application, do the following:
  1. Click Run > Debug Configurations.

    The Debug Configuration wizard opens.

  2. In the left pane of the Debug Configuration wizard, expand BusinessWorks Application and select BWApplication.
  3. In the Applications tab, if you have multiple applications, click Deselect All. Select the check box corresponding to tibco_bw_sample_binding_rest_basic_service_application.

    Doing so will also select tibco_bw_sample_binding_rest_basic_service.Books which is the service process.

  4. Click Debug. It runs the sample in Debug mode.

    After the command completes the console window shows engine messages similar to: Started BW Application [tibco_bw_sample_binding_rest_basic_service_application:1.0].

Obtaining the Discovery URL for the service

  1. In the Console View, press Enter to get the prompt.
  2. Run the OSGI command, lrestdoc to list the Discovery URL for the service's Swagger file. This command lists the following:

    [Application Name]: tibco_bw_sample_binding_rest_basic_service_application

    [Discovery Url]: http://<localhost>:7777/tibco_bw_sample_binding_rest_basic_service_application

Testing the Service
  1. Copy the Discovery URL to your clip board.
  2. Launch Google Chrome browser and enter the Discovery URL from the previous step into the browser's address bar.

    The Swagger API Page displays:

  3. Click the Books link to list the operations. To test the POST operation, click POST and provide the JSON payload for a book. You can use the books.json payload provided with the sample present at BW_HOME\bwcloud\<version>\samples\binding\rest\Basic\books.json.
  4. Click Try it out! You should see the book details that you posted in the Response Body and a Response Code of 200.
  5. Optionally, push your application to the cloud. For details on how to do this see Pushing an Application to the Cloud.

Cient Application

The client application consists of the Client.bwp process. This process is used to invoke the POST and GET operations on the Books REST resource.

Note: If you are accessing the sample from the TIBCO Business Studio Welcome page, go directly to Step 3.

To open the Client.bwp process, follow these steps:

  1. In File Explorer view, expand the samples > binding > rest > Basic folder and double-click tibco_bw_sample_binding_rest_basic_client.zip to expand the client project in the Project Explorer.

    This is a REST Client process which uses the REST Reference Binding to retrieve data from the REST Service. The Timer activity is configured to start the process and is followed by the Sleep activity. The Sleep activity is configured to make sure that the client process lies dormant until the service is started since the service needs to be running in order for the client to access it. The client process retrieves the data from the service in XML format by invoking the GET operation. It converts the data into JSON using the RenderJSON activity and then sends the converted data to the console using the Log activity. It invokes the POST operation to add one or more books to the book collection. The JSON payload for POST operation can be specified from the Swagger API page using the contents of books.json located at same level as of the sample.

    The books REST Reference binding which is created using books.json Swagger document has the following fields as shown below. All these fields are pre-populated. HTTP Client Shared Resource must listen to the same host and port as that of the HTTP Connector which is used by REST Service.

    You can click the get or post operation under Rest Reference to see its configuration.

Running the Client Application
Note: The service must be running in order for the client to access it.
  1. In Project Explorer expand the tibco_bw_sample_binding_rest_basic_client project.
  2. Fully expand the Processes directory and double-click Client.bwp to open it in the Process Editor pane.
  3. Click Run > Debug Configurations.

    The Debug Configuration wizard opens.

  4. In the left pane of the Debug Configuration wizard, expand BusinessWorks Application and select BWApplication.
  5. In the Applications tab, if you have multiple applications, click Deselect All. Select the check box corresponding to tibco_bw_sample_binding_rest_basic_client_application and tibco_bw_sample_binding_rest_basic_server_application.

    Doing so will also select tibco_bw_sample_binding_rest_basic_client.Client which is the client process and tibco_bw_sample_binding_rest_basic_service.Books which is the service process.

    You must select both the service application and the client application in order for the client to be able to access the service.

  6. Click Debug. It runs the sample in Debug mode.

    After the command completes the console window shows engine messages similar to: Started BW Application [tibco_bw_sample_binding_rest_basic_client_application:1.0] and Started BW Application [tibco_bw_sample_binding_rest_basic_service_application:1.0] indicating that both the client and the server have started successfully.

    You will also see the outputs of the Log and Log1 activities configured in the client process.

Troubleshooting

  • If you do not see the REST Swagger API page, verify whether the application has started.
  • If you see any problem markers in the project, clean the project by invoking Project > Clean or try switching to a clean new workspace.