Technical note book of Chatura Dilan

How to use WSO2 ESB – Payload Factory Mediator to change JSON service output

In this post I am going to show how to use mediate a simple JSON REST service using WSO2 ESB.

1. First let’s assume we have a JSON web service which returns following JSON output to the client.

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "user": "dilan"
}

2. But the client does not require the JSON output in the above format. He need to change the “user” to “username” as following.

{
   "name": "Chatura Dilan",
   "company": "WSO2",
   "username": "dilan"
}

3. In this case, we are going to use the ‘Payload Factory Mediator’ of WSO2 ESB to change the payload of the message.

You can host a sample service in any server which returns the output as following. Please make sure the Content Type of the JSON output is application/json

{
 "name": "Chatura Dilan",
 "company": "WSO2",
 "user": "dilan"
}

If you are using Apache PHP server you can create a test php file and add the following code there.

<?php
header('Content-type: application/json');
echo '{
 "name": "Chatura Dilan",
 "company": "WSO2",
 "user": "dilan"
}';

4. Now start the WSO2 ESB, login to the ESB and click on ‘API’ Menu in the ‘Service Bus’ tab

5. Click on ‘Add API’ to create a new API

6.  Add API Name and Context as Following and Click on ‘Add Resource’

7.  Now we are adding a ‘In Sequence’, the sequence which does the communicate between the server and the ESB.

Set Resource Method as GET and In Sequence as ‘Define Inline’. Click on the Create link

8. Now we need to send a message to the server to get the output. So we add a send mediator for that by selecting Add Child -> Core -> Send

9. In ‘Send Mediator’ window click on ‘Define Inline’ and click on ‘Add’ link

10. Select ‘Address Endpoint’ from the list

11. Add the server URL which returns the JSON as the Address

12. Click ‘Save & Close’

13 . Click ‘Update’ and ‘Save & Close’ again

14.  The XML view of ‘In Sequence’ as following


<inSequence xmlns="http://ws.apache.org/ns/synapse">
 <send>
 <endpoint>
 <address uri="http://localhost/test/test1.php"></address>
 </endpoint>
 </send>
</inSequence>

15. Now it’s time to create the ‘Out Sequence’. Which returns the message from the ESB to Client. So we are going to mediate the out sequence here. Select the ‘Out Sequence’ and Click on Create.

16. Now add a ‘PayloadFactory’ mediator By selecting Add Child -> Transform -> PayloadFactory

17. Please add the Payload format and Arguments as following

18. Also create the send mediator by setting the Endpoint to none.

The XML code of the ‘Out Sequence’  will be following.


<outSequence xmlns="http://ws.apache.org/ns/synapse">
 <payloadFactory media-type="json">
 <format>{ "name": "$1", "company": "$2", "username": "$3"}</format>
 <args>
 <arg expression="$.name" evaluator="json"></arg>
 <arg expression="$.company" evaluator="json"></arg>
 <arg expression="$.user" evaluator="json"></arg>
 </args>
 </payloadFactory>
 <send></send>
</outSequence>

19. Now click ‘Update’ , ‘Save & Close’, ‘Update’ and ‘Save’

20. That’s it. Access the  API invocation URL and you can see the output message is mediated by the WSO2 ESB

http://localhost:8280/mytest

now you can see the output response is mediated using WSO2 ESB


Leave a Reply

Your email address will not be published. Required fields are marked *