Monday, July 20, 2015

How to use Payload Factory in WSO2 ESB

Payload factory mediator is used to transform the message in desired output. It replaces the message of your content; it can be a request, response of any normal content. In the payload we can pass some argument to make it dynamic otherwise in payload we can pass static content as well.

<payloadFactory media-type="xml | json">
    <format ../>
        <arg (value="string" | expression=" {xpath} | {json} ")/>*

In above syntax payload factory is defined, which start with keyword ‘payloadFactory’. Inside of payload factory we have to pass format in which we can put any request, response, any structure of xml or any content. If format is dynamic then we have to pass argument to fill its data by <args> tag.

In above format media type can be either json or xml but by default media-type is xml. Ongoing message in payload factory can be changed by the below property.

<property name="messageType" value="application/json" scope="axis2"/>

Here outcome message is being changed in json format.


<payloadFactory media-type="xml">
                <emp:getEmployeeDetails xmlns:deal="">
                <arg expression="get-property('EmpName')"/>
                <arg expression="get-property('EmpPassword')"/>
        <endpoint key="DSS_EmployeeDetailsProxy_EPR"/>

In above example we are calling an external service as a part of service chaining using payload factory. This service will get employee details from the DSS_EmployeeDetailsProxy service which is a DSS service that is why prefix DSS is used.

In this example we are passing two arguments and if you look into the format tag, argument is getting filled by the $n values. In argument there are 2 values EmpName and EmpPassword and same in the request, we have two placeholder for that with value of $1 and $2, which will be replaced by the argument value.