...
Tip | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Before you begin,
|
...
- In the REST API configuration, select API Resource in the API palette and drag it onto the canvas just below the previous API resource that was created.
Click the API Resource you just added to access the Properties tab and fill in the following details:Anchor uriTemplate uriTemplate Url Style Click the down arrow and select URI_TEMPLATE from the list.
URI-Template Enter /categories/{category}/reserve
Methods Enable Post - Drag a Property mediator from the Mediators palette to the In Sequence of the API resource and name it Get Hospital.
This is used to extract the hospital name that is sent in the request payload. With the Property mediator selected, access the Properties tab and fill in the following details:
Property Name New Property...
New Property Name Hospital
Property Action set
Value Type EXPRESSION
Value Expression Click the Value Expression field in the Property tab and add the following expression:
json-eval($.hospital)
Info This is the JSONPath expression that will extract the hospital from the request payload.
Add a Switch mediator from the Mediator palette just after the Property Mediator.
Right-click the Switch mediator you just added and select Add/Remove Case to add the number of cases you want to specify.
In this scenario, we are assuming there are three different hospitals, hence there are three cases.Enter 3 for Number of branches and click OK.
With the Switch mediator selected, go to the Properties tab and fill in the details given below:
Source XPath The Source XPath field is where we specify the XPath expression, which obtains the value of the hospital that we stored in the Property mediator.
Follow the steps given below to specify the expression:
- Click in the Value field of the Source XPath property.
- Enter
get-property('Hospital')
as the XML value. - Click OK.
Info For more information on
get-property()
, see XPath Extension Functions.Case Branches Follow the steps given below to add the case branches:
- Double click the 'Case Regex' values listed under the Case Branches property.
- Change the RegExp values as follows for each of the Case branches:
- Case 1: grand oak community hospital
- Case 2: clemency medical center
- Case 3: pine valley community hospital
- Click OK.
Let's add a /wiki/spaces/EI6xx/pages/49612782 to print a message indicating to which hospital the request message is being routed.
Drag a Log mediator to the first Case box of the Switch mediator, and name it Grand Oak Log.Anchor SwitchLogMediator SwitchLogMediator With the Log mediator selected, access the Properties tab and fill in the information given in the table below:
Field Value Description Log Category INFO
Indicates that the log contains an informational message. Log Level CUSTOM
When Custom
is selected, only specified properties will be logged by this mediator.
For more information on the available log levels, see the Log Mediator .Log Separator (blank)
Since there is only one property that is being logged, we do not require a separator, so this field can be left blank. Properties Follow the steps given below to extract the stock symbol from the request and print a welcome message in the log.
- Click the Value field of the Properties property, and then click the browse (...) icon that appears.
- In the Log Mediator Configuration dialog, click New, and then add a property as follows:
- Name:
message
- Type:
EXPRESSION
(We selectEXPRESSION
because the required properties for the log message must be extracted from the request, which we can do using an XPath expression.) Value/Expression: Click the browse (...) icon in the Value/Expression field and enter
fn:concat('Routing to ', get-property('Hospital'))
.Info This XPath expression value gets the value stored in the Property mediator and concatenates the two strings to display the log following message:
Routing to <hospital name>
.
- Name:
- Click OK.
Add a Send mediator adjoining the Log mediator and add the GrandOakEP endpoint from Defined Endpoints palette to the empty box adjoining the Send mediator.
Add Log mediators in the other two Case boxes in Switch mediator and then enter the same properties as in Step 10.
Make sure to name the two Log mediators asClemency Log
andPine Valley Log
respectively.Add Send mediators adjoining these log mediators and respectively add the ClemencyEP and PineValleyEP endpoints from the Defined Endpoints palette.
Info You have now configured the Switch mediator to log the
Routing to <Hospital Name>
message when a request is sent to this API resource. The request message will then be routed to the relevant hospital backend service based on the hospital that is sent in the request payload.Add a Log mediator to the Default (the bottom box) of the Switch mediator and configure it the same way you did for the Log mediator above.
Note Make sure to name it Fault Log and changing its Value/Expression as follows:
fn:concat('Invalid hospital - ', get-property('Hospital'))
The default case of the Switch mediator handles the invalid hospital requests that are sent to the request payload. This logs the "
Invalid hospital - <Hospital Name>
" message for requests that have the invalid hospital name.- Drag a Respond mediator next to the Log mediator you just added.
This ensures that there is no further processing of the current message and returns the request message back to the client.
The In Sequence of the API resource configuration should now look like this: - Drag a Send mediator to the Out sequence of the API resource, to send the response back to the client.
- Save the updated REST API configuration.
...
- Download the JAR file of the back-end service.
Open a terminal and navigate to the location where your saved the back-end service.
Execute the following command to start the service:
Code Block java -jar Hospital-Service-JDK11-2.0.0-EI7.jar
The Healthcare service is now active and you can start sending requests to the service.
...