When you want to change the content of a message a PayLoadFactory Mediator is one way to do so. Basically what the PayloadFactory Mediator does is change (i.e. transform or replace) part of the message defined by format with the args defined later on.
Syntax
The syntax of the PayloadFactory mediator is as follows:
<payloadFactory media-type="xml | json">
<format ../>
<args>
<arg (value="string" | expression=" {xpath} | {json} ")/>*
</args>
</payloadFactory>
The media-type indicates the format of the message that is going to be changed, either JSON or XML. In the <format> section we indicate what the message will look like, basically mentioning the elements. The actual content will be added by the <args> part.
Three transformations
In this article we will perform three transformations on a message:
- Adding elements to the message
- Changing the date format from US notation (mm/dd/yyyy) to EU notation (dd/mm/yyyy)
- Renaming one field <date> to <date_ordered> and removing the <clientnr>
The xml that is send to the proxy is always the same (of course in a soap message)
order>
<ordernr>10291</ordernr>
<date>12/22/2015</date>
<clientnr>6690</clientnr>
</order>
Tools
To develop and test this simple example I use Development Studio 3.8.0 from WSO2 and SoapUI. Because this is a simple example without an associated backend service (it only shows the PayLoadFactory capabilities) I am using a Respond Mediator in order to send the changed message back.
In all cases the graphical overview of the three proxies is as follows:
Note that the Log Mediators are not even necessary, they are simply there to show message content on the console.
Changing dates
Again, the XML for this proxy is almost the same as the previous one:
The difference is that the date value is changed using an XPATH function called substring and concat that cuts and pastes the date field in the required format, in essence switching day and month.
As you can see the date is switched in the response message.
Renaming and removing
Finally, we rename the <date> field to <date_ordered> and remove the <clientnr>. This results in a minimal change in the PayLoadFactory Mediator.
The SoapUI response shows the message we just created.
The PayLoadFactory Mediator is one powerful way to transform messages, adding, deleting or changing the message or it’s values.
Working daily with WSO2 products we sometimes encounter things that even we did not know yet, but in fact are so handy we want to share it with the whole world. We have created a new group of articles called WSO-2-EASY that will show some of best tips and tricks that will help you to either save time or create tidier code. | |
Yenlo is the leading, global, multi-technology integration specialist in the field of API-management, Integration technology and Identity Management. Known for our strong focus on best-of-breed hybrid and cloud-based iPaaS technologies.
Yenlo is the product leader and multi-award winner in WSO2, Boomi, MuleSoft and Microsoft Azure technologies and offers best-of-breed solutions from multiple leading integration vendors.
With over 240+ experts in the API, integration, and Identity Access Management domain and over $35 million in annual revenue, Yenlo is one of the largest and best API-first and Cloud-first integration specialists worldwide.