Skip to content

How to Route Requests Based on Message Content

What you'll build

In this tutorial, we are creating the mediation artifacts that can route a message to the relevant backend depending on the content of the message payload.

When the client sends the appointment reservation request to the Micro Integrator, the message payload of the request contains the name of the hospital where the appointment needs to be confirmed. The HTTP request method that is used for this is POST. Based on the hospital name sent in the request message, the Micro Integrator should route the appointment reservation to the relevant hospital's back-end service.

Concepts and artifacts used

Let's get started!

Step 1: Set up the workspace

You need Visual Studio Code (VS Code) with the Micro Integrator for VS Code extension installed.

Info

See the Install Micro Integrator for VS Code documentation to learn how to install Micro Integrator for VS Code.

Step 2: Develop the integration artifacts

Follow the instructions given in this section to create and configure the required artifacts.

Create an integration project

The Integration project will contain all the required artifacts for the integration solution.

  1. Launch VS Code with the Micro Integrator extension installed.

  2. Click on the Micro Integrator icon on the Activity Bar of the VS Code editor.

    MI VS Code Extension

  3. Click Create New Project on Design View.

    Design View Pane Create New Project

    Next, the Project Creation Form will be opened.

  4. In the Project Creation Form, enter ContentBasedRouteTutorial as the Project Name.

  5. Provide a location under the Project Directory.

    create new project

  6. Click Create.

Once you click Create, the Add Artifact pane will be opened.

Create HTTP connections

In this tutorial, we have three hospital services hosted as the backend:

  • Grand Oak Community Hospital: http://localhost:9090/grandoaks/
  • Clemency Medical Center: http://localhost:9090/clemency/
  • Pine Valley Community Hospital: http://localhost:9090/pinevalley/

The request method is POST and the format of the request URL expected by the back-end services is as below. http://localhost:9090/grandoaks/categories/{category}/reserve

Let's create three different HTTP connections for the above services.

  1. Navigate to MI Project Explorer.

  2. Click on + Add artifact.

    add artifact

  3. Click + View More under Create an Integration.

  4. Select Connections under Other Artifacts to open the Connector Store Form.

    connections artifact

  5. Select HTTP.

    select HTTP connection

  6. In the Add New Connection form, specify the following values to create the new HTTP connection.

    Property Value Description
    Connection Name GrandOakConn The name of the connection.
    Base URL http://localhost:9090/grandoaks The base of the request URL for the back-end service.

  7. Click Add.

  8. Similarly, create the HTTP connections for the other two hospital services using the Base URLs given below:

    Connection Name Base URL
    ClemencyConn http://localhost:9090/clemency
    PineValleyConn http://localhost:9090/pinevalley

You have now created the three connections for the hospital back-end services that will be used to make appointment reservations.

Tip

You can also create a single connection where the differentiation of the hospital name can be handled using a variable in the relative path of an operation. See the tutorial on Exposing Several Services as a Single Service.

Using three different connections is advantageous when the back-end services are very different from one another and/or when there is a requirement to configure error handling differently for each of them.

Create a REST API

  1. In the MI Project Explorer click on + Add artifact.
  2. Select API under Create an Integration.
  3. Enter the details given below to create a new REST API.

    Property Value Description
    Name HealthcareAPI The name of the REST API.
    Context /healthcare Here you are anchoring the API in the /healthcare context. This will become part of the name of the generated URL used by the client when sending requests to the Healthcare service. For example, setting the context to /healthcare means that the API will only handle HTTP requests where the URL path starts with http://host:port/healthcare.

    Synapse API artifact

  4. Click Create.

  5. On the Service Designer, click on the three dots (â‹®) and then Edit to access the Properties of the default API resource.

    Edit API resource

  6. Enter the following details:

    Property Description
    Resource Path /categories/{category}/reserve
    Methods POST

    edit API resource

  7. Click Update.

Define the mediation flow

You can now start configuring the API resource.

  1. Navigate to MI Project Explorer > APIs > HealthcareAPI > /categories/{category}/reserve to open the Resource View.

  2. Click on the + icon to open the Mediator Palette.

    open palette

  3. Select the Switch mediator under Mediators.

  4. In the Add Switch Mediator pane that appears, specify the following values.

    Property Description
    Expression

    The Expression field is where you specify the Synapse expression used to extract the hospital name from the request payload.

    Follow the steps given below to specify the expression:

    1. Click the EX button at the end of the Value field.
    2. Enter payload.hospital.
    Cases

    Use the + Add New Case button to add each of the following case branches by setting the following Regex patterns:

    1. grand oak community hospital
    2. clemency medical center
    3. pine valley community hospital

  5. Click Add to insert the Switch mediator to the integration flow.

  6. Add a Log mediator to each case branch by clicking on the + icon in the respective case branch and selecting the Log mediator from the Mediator Palette. In the pane that appears, specify the following values.

    Case 1 (grand oak community hospital) Case 2 (clemency medical center) Case 3 (pine valley community hospital) Default
    Log Category INFO INFO INFO ERROR
    Message Routing to: ${payload.hospital} Routing to: ${payload.hospital} Routing to: ${payload.hospital} Invalid hospital: ${payload.hospital}
    Description GrandOak Log Clemency Log PineValley Log Fault Log

    Resource view after adding log

    Info

    You have now configured the Switch mediator to log the message Routing to: <Hospital Name> whenever a request is sent to this API resource. The request will then be routed to the relevant hospital back-end service based on the hospital name provided in the request payload.

    The default case of the Switch mediator handles invalid hospital requests. It logs the message Invalid hospital: <Hospital Name> for requests with an invalid or unrecognized hospital name.

  7. Add an HTTP POST operation by clicking the + icon after the Log mediator in each case branch, except for the default branch. In the pane that appears, specify the following values.

    Tip

    You can search for post in the Mediator Palette to quickly find the HTTP POST operation.

    Case 1 (grand oak community hospital) Case 2 (clemency medical center) Case 3 (pine valley community hospital)
    Connection GrandOakConn ClemencyConn PineValleyConn
    Relative Path /categories/${params.pathParams.category}/reserve /categories/${params.pathParams.category}/reserve /categories/${params.pathParams.category}/reserve
    Response Variable Name hospital_res hospital_res hospital_res

    Note

    We will leave the rest of the configurations as defaults: Content Type set to JSON, Request Body as ${payload}, and Overwrite Message Body checked.

    HTTP POST operation

  8. Add a Respond mediator right after the Switch mediator to return the response from the healthcare service back to the client.

    You have successfully created all the artifacts required to route messages to a back-end service based on the content of the request payload.

    Resource view

Step 3: Build and run the artifacts

Now that you have developed an integration using the Micro Integrator for the Visual Studio Code plugin, it's time to deploy the integration to the Micro Integrator server runtime.

Click the Build and Run icon located in the top right corner of VS Code.

Build and Run

Step 4: Test the use case

Let's test the use case by sending a simple client request that invokes the service.

Start the back-end service

  1. Download the JAR file of the back-end service from here.
  2. Open a terminal, and navigate to the location where you saved the back-end service.
  3. Execute the following command to start the service:

    java -jar Hospital-Service-JDK11-2.0.0.jar
    

Send the client request

Now, let's test the integration service. For that, you can use the inbuilt try-it functionality in the MI for VS Code extension.

When you run the integration artifact as in Step 3, the Runtime Services interface is opened up. You can see all the available services.

Select the HealthcareAPI you have developed and test the resource using the following category and payload.

Category surgery
Payload
{ "patient": { "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "doctor": "thomas collins", "hospital_id": "grandoaks", "hospital": "grand oak community hospital", "appointment_date": "2025-04-02" }

This JSON payload contains details of the appointment reservation, which includes patient details, doctor, hospital, and data of appointment.

Try Out

Optionally, you can use Postman or cURL to send the request. You can refer to the following request information.

Method POST
Headers Content-Type=application/json
URL http://localhost:8290/healthcare/categories/surgery/reserve

The URI-Template format that is used in this URL was defined when creating the API resource: http://host:port/categories/{category}/reserve
Body
{ "patient": { "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "doctor": "thomas collins", "hospital_id": "grandoaks", "hospital": "grand oak community hospital", "appointment_date": "2025-04-02" }

Analyze the response

You will see the following response received to your HTTP Client:

{
  "appointmentNumber": 1,
  "doctor": {
    "name": "thomas collins",
    "hospital": "grand oak community hospital",
    "category": "surgery",
    "availability": "9.00 a.m - 11.00 a.m",
    "fee": 7000
  },
  "patient": {
    "name": "John Doe",
    "dob": "1940-03-19",
    "ssn": "234-23-525",
    "address": "California",
    "phone": "8770586755",
    "email": "[email protected]"
  },
  "fee": 7000,
  "confirmed": false
}

Now, check the Output tab in VS Code. You should see a message similar to the following: INFO {LogMediator} - {api:HealthcareAPI POST /healthcare/categories/surgery/reserve} Routing to: grand oak community hospital.

This message is printed by the Log mediator when the client request is routed to the appropriate backend using the Switch mediator.

You have successfully completed this tutorial and learned how requests received by the Micro Integrator can be routed to the relevant backend using the Switch mediator