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.
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
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:
- In the File Explorer view, expand samplesbindingrestPetStore and double-click petstore_sample_service.zip to expand it in the Project Explorer.
- In Project Explorer, expand the petstore_sample_service project.
- Fully expand the Processes directory.
- 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.
- 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 the 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 that 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:
- In the Process Editor, click .
- In the Debug Configuration wizard, expand BusinessWorks Application in the left pane and select BWApplication.
- Click the
Applications tab and then click the
Deselect All button if you have multiple applications open. Select the checkbox next to
petstore_sample_service_application.
This will automatically check the petstore_sample_service.Activator and petstore_sample_service.Pet processes.
- Click
Debug. This runs 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
- 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.
- Copy the Discovery URL to your clip board.
Testing the Service
To test the service, follow these steps:
- Open the Google Chrome browser and enter the Discovery URL from the step above. The Swagger API Page displays:
- Click the List Operations link to expose the operations that the application supports.
- Operations for the application listed in the Swagger API Page can be used as follows:
- 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 get the following response indicating that the POST was successful:
Click
to view the Pet information that you posted in the previous step. You see a similar response:- Similarly, PUT and DELETE operations can be invoked from the Swagger API Page.
- 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.
- 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 that is used by a REST service.
To run the client application, follow these steps:
- In the Process Editor, click .
- In the Debug Configuration wizard, expand BusinessWorks Application in the left pane and select BWApplication.
- Click the
Applications tab and then click the
Deselect All button if you have multiple applications open. Select the checkbox next to
petstore_sample_service_application and
petstore_sample_client_application.
Run both the service and the client application in order for the client to access the service.
- Click
Debug. This runs 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 or try switching to a clean new workspace.