This documentation is for WSO2 Enterprise Service Bus version 5.0.0. For the latest ESB, view the latest WSO2 Enterprise Integrator documentation.
||
Skip to end of metadata
Go to start of metadata

Message transformation is necessary when the message format sent by the client is different from the message format expected by the back-end service. The Message Translator architectural pattern in WSO2 ESB describes how to translate from one data format to another.

In this tutorial , you send a request message to a back-end service where the payload is in a different format when compared to the request payload expected by the back-end service. Data Mapper mediator in WSO2 ESB is used to transform the request message payload to the format expected by the back-end service.

 

See the following topics for a description of the concepts that you need to know when creating ESB artifacts:

 

Before you begin,

  1. Install Oracle Java SE Development Kit (JDK) version 1.8.* and set the JAVA_HOME environment variable.
  2. Go to http://wso2.com/products/enterprise-service-bus/, click DOWNLOAD to download the ESB runtime ZIP file, and then extract the ZIP file. 
    The path to this folder will be referred to as <ESB_HOME> throughout the tutorials.
  3. Go to http://wso2.com/products/enterprise-service-bus/, click Tooling to select and download the relevant ESB tooling ZIP file, and then extract the ZIP file. 
    The path to this folder will be referred to as <TOOLING_HOME> throughout the tutorials. 

    For more detailed installation instructions, see the Installing WSO2 ESB Tooling.

  4. Open the ESB Tooling environment and click File -> Import. Then, select Existing WSO2 Projects into workspace under the WSO2 category, click Next and upload the pre-packaged C-App project. This C-App contains the configurations of the  previous tutorial so that you do not have to repeat those steps.


Let’s assume this is the format of the request sent by the client:

 

{
  "name": "John Doe",
  "dob": "1940-03-19",
  "ssn": "234-23-525",
  "address": "California",
  "phone": "8770586755",
  "email": "johndoe@gmail.com",
  "doctor": "thomas collins",
  "hospital": "grand oak community hospital"
}

 

However, the format of the message must be as follows to be compatible with the backend service.

 

{
  "patient": {
    "name": "John Doe",
    "dob": "1990-03-19",
    "ssn": "234-23-525",
    "address": "California",
    "phone": "8770586755",
    "email": "johndoe@gmail.com"
  },
  "doctor": "thomas collins",
  "hospital": "grand oak community hospital"
}

 

The client message format must be transformed to the back-end service message format within the In sequence.

Let's get started!

This tutorial contains the following sections:

Creating the deployable artifacts

The Data Mapper mediator transforms the message within the In sequences. The Data Mapper mediator is a data mapping solution that can be integrated into a mediation sequence. It converts and transforms one data format to another, or changes the structure of the data in a message. 

  1. In WSO2 ESB Tooling, add a Data Mapper mediator just after the Property mediator in the In sequence of the API resource.

  2. Double click the Data Mapper mediator icon and provide the following name for the data mapping configuration file that will be created.

    • Configuration Name: RequestMapping

    The SampleServicesRegistry project is created at the time of creating the ESB Solution project and will get auto picked here.

    Click OK. You view the data mapping editor.

  3. Create a JSON file by copying the following sample content of the request message sent to the API Resource, and save it in your local file system.

    {  "name": "John Doe",
      "dob": "1990-03-19",
      "ssn": "234-23-525",
      "address": "California",
      "phone": "8770586755",
      "email": "johndoe@gmail.com",
      "doctor": "thomas collins",
      "hospital": "grand oak community hospital"
    }

    You can  create a JSON schema manually  for input and output using the Data Mapper Diagram editor.

  4. Right-click on the top title bar of the Input box and click Load Input  as shown below

  5. Select JSON as the Resource Type as shown below.

  6. Click the file system link in Select resource from, select the JSON file you saved in your local file system in step 5, and click Open
    You view the input format loaded in the Input box in the editor as shown below. 


  7.  Create another JSON file by copying the following sample content of the request message expected by the backend service, and save it in your local file system.  

    {
      "patient": {
        "name": "John Doe",
        "dob": "1990-03-19",
        "ssn": "234-23-525",
        "address": "California",
        "phone": "8770586755",
        "email": "johndoe@gmail.com"
      },
      "doctor": "thomas collins",
      "hospital": "grand oak community hospital"
    }
  8. Right-click on the top title bar of the Output box and click Load Output as shown below. 
    load output message format
  9. Select JSON as the resource type
  10. Click the file system link in Select resource from, select the JSON file you saved in your local file system in step 7, and click Open.  
    You view the input format loaded in the Output box in the editor as shown below. 

    Check the Input and Output boxes with the sample messages, to see if the element types (i.e. (Arrays, Objects and Primitive values) are correctly identified or not. Following signs will help you to identify them correctly. 

    • {} - represents object elements
    • [] - represents array elements
    • <> - represents primitive field values
    • A - represents XML attribute value
  11. Do the mapping by dragging arrows from field values in the input box to the relevant field values in the output box. The final mapping is as follows:
  12. Save and close the configuration.

  13. Go back to the Design View of the API Resource and select the Data Mapper Mediator and edit the following in the Properties tab:

    • Input Type: JSON
    • Output Type: JSON

  14. Save the REST API configuration.

We are now ready to package and deploy the C-App and send the request.

Deploying the Artifacts to WSO2 ESB

Since we created a new Registry Resource project, this will need to be packaged into our existingCApp.

  1. Package the C-App names SampleServicesCompositeApplication project with the artifacts created. 

    Ensure the following artifact check boxes are selected in the Composite Application Project POM Editor.

    • SampleServices
      • HealthcareAPI
      • ClemencyEP
      • GrandOakEP
      • PineValleyEP
    • SampleServicesRegistry
  2. On the Servers tab, expand the WSO2 Carbon server, right-click SampleServicesCompositeApplication, and choose Redeploy. The Console window will indicate that the CApp has been deployed successfully.

    If you do not have a server added in Eclipse, refer this tutorial.

    You can also deploy the artifacts to the ESB server using a Composite Application Archive (CAR) file.

Sending requests to WSO2 ESB

  1. Create a JSON file names request.json with the following request payload.

    {
    "name": "John Doe",
    "dob": "1990-03-19",
    "ssn": "234-23-525",
    "address": "California",
    "phone": "8770586755",
    "email": "johndoe@gmail.com",
    "doctor": "thomas collins",
    "hospital": "grand oak community hospital"
    }
  2. Open a command line terminal and execute the following command from the location where request.json file you created is saved:

    curl -v -X POST --data @request.json http://localhost:8280/healthcare/categories/surgery/reserve --header "Content-Type:application/json"

    This is derived from the URI-Template defined when creating the API resource.

    http://<host>:<port>/categories/{category}/reserve

    You will see the response as follows:

    {"appointmentNumber":2,
      "doctor":
           {"name":"thomas collins",
            "hospital":"grand oak community hospital",
            "category":"surgery","availability":"9.00 a.m - 11.00 a.m",
            "fee":7000.0},
      "patient":
          {"name":"John Doe",
           "dob":"1990-03-19",
           "ssn":"234-23-525",
           "address":"California",
           "phone":"8770586755",
           "email":"johndoe@gmail.com"},
      "fee":7000.0,
      "confirmed":false}

You have now explored how WSO2 ESB can receive a message in one format and transform it into the format expected by the backend service using the Data Mapper mediator.

  • No labels