Implementing Multiple Response based REST Service using a JSON file
This REST Sample shows how to use the OAS 3.0 JSON file to create a RESTful service which has operations configured with multiple responses.
The sample uses the POST HTTP method for the REST resource.
- Procedure
-
In the samples directory, select binding > rest > MultipleResponse and double-click tibco.bw.sample.binding.rest.MultipleResponse. For more information, see the Accessing Samples section.
-
In the Project Explorer, expand the tibco.bw.sample.binding.rest.MultipleResponse project.
-
Set the default application profile to match the operating system you are using. For more information, see Setting the Default Application Profile.
-
Fully expand the Processes directory and double-click Service.bwp.
-
Click Run > Debug Configurations.
-
At the left side pane of the Debug Configuration wizard, expand BusinessWorks Application and select BWApplication.
-
Click the Applications tab and then click the Deselect All button, if you have multiple applications. Select the checkbox next to tibco.bw.sample.binding.rest.MultipleResponse.application.
-
Click Debug. This runs the sample in the debug mode.
The console window shows engine messages similar to:
Started BW Application [tibco.bw.sample.binding.rest.MultipleResponse.application:1.0].
The OSGI command to list REST and Swagger URLs is
lrestdoc, which lists the following discovery URL:[Application Name]: tibco.bw.sample.binding.rest.MultipleResponse.application1.0[Discovery Url]: http://TIBCO-PF372WXP:7777/tibco.bw.sample.binding.rest.MultipleResponse.application
-
Launch the Google Chrome browser and open
http://TIBCO-PF372WXP:7777/tibco.bw.sample.binding.rest.MultipleResponse.application..The Swagger API Page displays as follows:
-
Click the POST operation displayed on the web page.
-
After completing the operations on the page, in TIBCO Business Studio for BusinessWorks, click the Terminate icon (
) to stop the application.
-
200 Status Code with an image/jpeg and text/plain format.
-
400 Status Code with application/xml format.
-
503 Status Code with application/json format.
We have to select the Response Media Type from the dropdown for the 200 status code.
On selecting image/jpeg, we can receive the image file which was sent in the request as a form parameter. The Image file is displayed in the Swagger UI.
On selecting text/plain, we receive a string response similar to the following :
Successful response with simple string
For fault status codes, we have to use the Postman client, since the Accept headers can be controlled only by the 200 response media types in the Swagger UI.
In Postman we can pass the Accept header value corresponding to the content-type which we are expecting in response. In our sample, if we want to receive a response in XML format, we can pass the Accept header value as application/xml or application/json for JSON format.
| • | For application/json Accept header we receive a response similar to the following : |
{"msgCode":503,"message":"Replying with custom fault as file not found."}
| • | For application/xml Accept header we receive a response similar to the following : |
<client-400-application-xml>Sending 400 fault as file not found.</client-400-application-xml>
The output.log file is generated with the following information :
**********Multiple Response Output**********
Output of POST with text/plain Content-Type --> Successful response with simple string************
Caught 503 fault with application/json Content-Type --> {"msgCode":503,"message":"Replying with custom fault as file not found."}************
Caught 400 fault with application/xml Content-Type --> Sending 400 fault as file not found.************
Understanding the Configuration
The project contains two processes: Service and Client.bwp.
Service Process : In this process, the REST service performs the post operation. It uploads an image file and fetches it in response for image/jpeg content-type. This service is created using the MultipleResponse_Swagger.json file. The response to be sent to the client is decided if the condition on Transition is satisfied. The condition checks the Accept header sent by the client. If it matches image/jpeg or text/plain then service responds with the 200 status code. If this condition is not satisfied i.e we pass application/json or application/xml Accept header, then the invalid file is not found and the corresponding fault reply is sent from the Catch block based on transition check for Accept header.
Client Process : This is a REST Client process which uses the REST Invoke activity to retrieve data from the REST Server. An image file is uploaded using the form parameter and the Accept header value is changed on each iteration of the For Each loop. The Accept header takes the value from the loop Variables. Once the 503 fault is caught in the Catch block, we have an Invoke which passes the application/xml Accept header and its corresponding 400 fault is caught by another Catch block.
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 clicking Project > Clean or try switching to a clean new workspace.
-
If you get any File was not found exception, verify whether the
input_image.jpgfile is present at the location as described in the Input_file Module Property.