Search Results

Managing PetStore Using REST

This REST PetStore sample shows how to use the RESTful service to add, delete, update, and retrieve Pet information from a PetStore.

The Schema definitions for this sample is generated from a PetStore Swagger document obtained from http://petstore.swagger.io/v2/swagger.json and the implementation is a subset of PetStore application hosted at http://petstore.swagger.io/.

This sample uses the following HTTP methods for the Pet REST Resource:

  • POST: Add a Pet to the PetStore
  • GET: Get a Pet from the PetStore
  • PUT: Update a Pet in the PetStore
  • DELETE: Delete a Pet from the PetStore

Prerequisites

Install the latest Google Chrome version in the standard default location.

PetStore Service Application

The PetStore service application contains two processes:

  • Activator.bwp
  • Pet.bwp

Viewing the PetStore Service Processes

You can view the Activator.bwp and Pet.bwp process in the Process Editor. To do so, follow these steps:

Note: If you are accessing the sample from the TIBCO Business Studio Welcome page, go directly to Step 3.
  1. In the File Explorer view, expand samplesbindingrestPetStore and double-click petstore_sample_service.zip to expand it in the Project Explorer.
  2. In Project Explorer, expand the petstore_sample_service project.
  3. Fully expand the Processes directory.
  4. Double-click Activator.bwp to open it in the Process Editor as follows:

    The Activator process is used to define the workflow for initialization and shutdown related to the BusinessWorks engine execution. onStartup and onShutdown are two constructor operations created to perform tasks when an application starts or after an application stops. SetSharedVariable is used to define the Pet resource information in the onStartup constructor. This variable is mapped to the existing PetStore schema definition in the project and used across other processes. LogEnd activity is used in the onShutdown constructor operation to indicate the shutdown of the application.

  5. Double-click Pet.bwp to view the process in the Process Editor as follows:

    In this process, the REST service is configured to perform four operations, POST, GET, PUT, and DELETE. The POST operation response schema is defined using shared variables and is used to post the Pet information to the PetStore collection. The GET operation is used to retrieve specific Pet information from the PetStore. For the GET operation GetSharedVariable activity is mapped to getOut Reply activity to access first Pet from the collection. The PUT operation is used to update the specific attributes that are related to a Pet. The DELETE operation is used to remove a specific Pet information from the PetStore collection. All operation data is pre-configured using GetSharedVariable/SetSharedVariable activities. After you create a REST service, a Pet.json Swagger 2.0 document is generated in the Service Descriptors folder which can be used to create a REST reference binding to invoke REST service operations.

Running the PetStore Service Application

To run the PetStore service processes from the Process Editor, follow these steps:

  1. In the Process Editor, click Run > Debug Configurations.
  2. In the Debug Configuration wizard, expand BusinessWorks Application in the left pane and select BWApplication.
  3. Click the Applications tab and then click the Deselect All button if you have multiple applications open. Select the check box next to petstore_sample_service_application.

    This will automatically check the petstore_sample_service.Activator and petstore_sample_service.Pet processes.

  4. Click Debug. This will run the service in Debug mode.

    The console window shows engine messages similar to: Started BW Application [petstore_sample_service_application:1.0].

Obtaining the Discovery URL for the service

  1. Once the Debug command completes, press Enter to get a prompt. At the prompt, enter lrestdoc, the OSGI command to list REST and Swagger URLs.

    The output of the command lists a Discovery URL similar to [Application Name]: petstore_sample_service_application [Discovery Url]: http://localhost:7777/petstore_sample_service_application.

  2. Copy the Discovery URL to your clip board.

Testing the Service

To test the service, follow these steps:

  1. Launch Google Chrome browser and enter the Discovery URL from the step above. The Swagger API Page displays:

  2. Click the List Operations link to expose the operations that the application supports.
  3. Operations for the application listed in Swagger API Page can be used as follows:
    1. Click POST and provide the JSON payload. You can use the Pet.json payload provided with the sample located in BW_HOME\samples\binding\rest\PetStore\Pet.json.

      You will get the following response indicating that the POST was successful:

    2. Click GET > Try it out to view the Pet information that you posted in the previous step. You will see a similar response:

    3. Similarly, PUT and DELETE operations can be invoked from the Swagger API Page.
  4. Optionally, push the PetStore service application to the cloud. For details on how to do this see Pushing an Application to the Cloud.

PetStore Client Application

The REST Client process uses the REST Reference Binding and Invoke activity to retrieve data from the REST Service. This process is used to invoke the GET, POST, PUT (update), and DELETE operations on the REST resource. The Swagger API page can also be used to invoke a running REST Service for response. The JSON payload for POST operation can be specified from the Swagger API page using the contents of the Pet.json file located at the same level as the sample.

The pet-petID reference binding fields are pre-configured in this sample. HTTP Client Shared Resource must listen to the same host and port as that of the HTTP Connector which is used by REST service.

To run the client application, follow these steps:

  1. In the Process Editor, click Run > Debug Configurations.
  2. In the Debug Configuration wizard, expand BusinessWorks Application in the left pane and select BWApplication.
  3. Click the Applications tab and then click the Deselect All button if you have multiple applications open. Select the check box next to petstore_sample_service_application and petstore_sample_client_application.

    You must run both the service and client application in order for the client to access the service.

  4. Click Debug. This will run the service in Debug mode.

    The console window displays the logs for each of the operations that the client invokes.

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.