Powered By Blogger

Feb 6, 2014

Payload Factory Mediators to work with JSON and XML Messages


This blog posts basically describes how we can use payload factory mediators for message mediation. Mainly it can be used to transform a message or replace the content of a message. Basically we can configure the format of the request or response in such away which maps with the arguments provided. 

Attributes:

  •  media-type: Mainly this can be either XML or JSON. The default is XML. Also if you want to change it to JSON then you need to add the message Type property after the </payloadfactory> tag
<property name="messageType" value="application/json" scope="axis2"/>

  • Payload Format: 
                      - Inline : This defines the payload to be added in the text field appears. It means                                        we have to add variables as $1, $2, etc. 

                      - Registry Reference: We can select a format stored in the Configuration or                                                                          Governance.

  • Arguments - For each variable in the format definition, we can add arguments to define the actual values. Mainly have to consider following the same order as in the format.


XML Example

<payloadFactory media-type="xml" description="">
        <format>
            <p:createrequest xmlns:p="http://customer.axis.org">
                <p:customerID>$1</p:customerID>
                <p:requestID>$2</p:requestID>
                <p:movieName>$3</p:movieName>
                <p:noOfRequestedSeats>$4</p:noOfRequestedSeats>
            </p:createrequest>
        </format>
        <args>
            <arg evaluator="xml" expression="//jsonObject/request/title/customerID/text()"/>
            <arg evaluator="xml" expression="//jsonObject/request/title/requestID/text()"/>
            <arg evaluator="xml" expression="//jsonObject/request/title/movieName/text()"/>
            <arg evaluator="xml" expression="//jsonObject/request/title/noOfRequestedSeats/text()"/>
        </args>
    </payloadFactory>

In this example the media-type is 'xml' and the format is inline and it represents the xml format. In arguments it takes JSON requests and have assigned those values to the payload defined in the format. Mainly in here the first argument matches with the first variable defined in the format etc.

JSON Example 

    <payloadFactory media-type="json">
<format>
{
"_get_login_json": {
"UName": "$1",
"password": "$2"
}
}
</format>

<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"
evaluator="xml" expression="//jsonObject/login/title/UName/text()" />
<arg xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"
evaluator="xml" expression="//jsonObject/login/title/password/text()" />
</args>
    </payloadFactory>

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

In this example the media-type is 'json' and the format is inline and it represents the json format. In arguments it takes JSON requests and have assigned those values to the payload defined in the format. Mainly in here the first argument matches with the first variable defined in the format etc.

1 comment: