First the payload needs to be set as a string property as below :
<property name="JSONPayload" expression="json-eval($.)"/>This property can then be used for any transformations as a string literal of the JSON.
Check out the sample below, which gets the response from the <call> mediator as a JSON, saves it to a string and then uses it in an xml payload. The final response of the API is XML.
<api xmlns="http://ws.apache.org/ns/synapse" name="JSONTestAPI" context="/json"> <resource methods="GET"> <inSequence> <call> <endpoint key="MockBackend"></endpoint> </call> <property name="JSONPayload" expression="json-eval($.)"></property> <log level="full"> <property name="====JSONPayload====" expression="$ctx:JSONPayload"></property> </log> <payloadFactory media-type="xml"> <format> <xmlPayload xmlns=""> <jsonPayload>$1</jsonPayload> </xmlPayload> </format> <args> <arg evaluator="xml" expression="$ctx:JSONPayload"></arg> </args> </payloadFactory> <property name="messageType" value="application/xml" scope="axis2"></property> <respond></respond> </inSequence> </resource> </api>
Response from mock backend is as below :
{ "mock":"mock service" }
JSON payload will be logged as below when invoking the API. Notice that although the JSON payload is built to XML, the property preserves the payload as a string literal.
LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:08b4f213-0df0-4d18-88b7-8cf3e2d1872c, Direction: request, ====JSONPayload==== = {
"mock":"mock service"
}, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><mock>mock service</mock></jsonObject></soapenv:Body></soapenv:Envelope><xmlPayload> <jsonPayload>{"mock":"mock service"}</jsonPayload> </xmlPayload>Additional Reference :
http://stackoverflow.com/questions/27848342/getpayloadjson-returning-an-empty-object
No comments:
Post a Comment