I am using WSO2 ESB 4.6.0 version to demonstrate this sample, simple yet powerful.
You do not need to write your own code or relay on other library to convert json to xml. Simple WSO2 esb proxy can do the conversion for you, so we will try that.
Lets set the requirement is to use a parameter to the methods to control whether to return JSON or XML.
Given synapse configuration below has the very basic configuration to solve this requirement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<definitions xmlns="http://ws.apache.org/ns/synapse"> | |
<registry provider="org.wso2.carbon.mediation.registry.WSO2Registry"> | |
<parameter name="cachableDuration">15000</parameter> | |
</registry> | |
<sequence name="fault"> | |
<log level="full"> | |
<property name="MESSAGE" value="Executing default 'fault' sequence"/> | |
<property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/> | |
<property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/> | |
</log> | |
<drop/> | |
</sequence> | |
<sequence name="main"> | |
<in> | |
<log level="full"/> | |
<filter source="get-property('To')" regex="http://localhost:9000.*"> | |
<send/> | |
</filter> | |
</in> | |
<out> | |
<send/> | |
</out> | |
<description>The main sequence for the message mediation</description> | |
</sequence> | |
<api name="JsonToXmlApi" context="/Test"> | |
<resource methods="POST" uri-template="/xml/"> | |
<inSequence> | |
<property name="messageType" value="text/xml" scope="axis2"/> | |
<header name="To" action="remove"/> | |
<property name="RESPONSE" value="true" scope="default" type="STRING"/> | |
<send/> | |
</inSequence> | |
</resource> | |
<resource methods="POST" uri-template="/json/"> | |
<inSequence> | |
<property name="messageType" value="application/json" scope="axis2"/> | |
<header name="To" action="remove"/> | |
<property name="RESPONSE" value="true" scope="default" type="STRING"/> | |
<send/> | |
</inSequence> | |
</resource> | |
</api> | |
</definitions> |
"JsonToXmlApi" rest api has two resource in the "Test" context. "/xml/" resource will covert the incoming json payload into xml format and send that back to client. whereas, "/json/" resource without converting which will echo back to the client.
You can test this using curl commands that have given below,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -i -H "Content-Type: application/json" -X POST -d '{ | |
"peoples": { | |
"people": [ | |
{ | |
"class": "Person", | |
"name": "vanjikumaran", | |
"birthday": "today", | |
"nickname": "Oracle of Omaha", | |
"phonNumbers": { | |
"phonNumber": [ | |
{ | |
"class": "Phone", | |
"name": "cell", | |
"number": "555-123-4567" | |
}, | |
{ | |
"class": "Phone", | |
"name": "home", | |
"number": "555-987-6543" | |
}, | |
{ | |
"class": "Phone", | |
"name": "work", | |
"number": "555-678-3542" | |
} | |
] | |
} | |
}, | |
{ | |
"class": "Person", | |
"name": "Steven Jobs", | |
"birthday": "Yesterday", | |
"nickname": "Steve", | |
"phonNumbers": { | |
"phonNumber": [ | |
{ | |
"class": "Phone", | |
"name": "cell", | |
"number": "555-123-4567" | |
}, | |
{ | |
"class": "Phone", | |
"name": "home", | |
"number": "555-987-6543" | |
}, | |
{ | |
"class": "Phone", | |
"name": "work", | |
"number": "555-678-3542" | |
} | |
] | |
} | |
} | |
] | |
} | |
}' http://localhost:8280/Test/json/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -i -H "Content-Type: application/json" -X POST -d '{ | |
"peoples": { | |
"people": [ | |
{ | |
"class": "Person", | |
"name": "vanjikumaran", | |
"birthday": "today", | |
"nickname": "Oracle of Omaha", | |
"phonNumbers": { | |
"phonNumber": [ | |
{ | |
"class": "Phone", | |
"name": "cell", | |
"number": "555-123-4567" | |
}, | |
{ | |
"class": "Phone", | |
"name": "home", | |
"number": "555-987-6543" | |
}, | |
{ | |
"class": "Phone", | |
"name": "work", | |
"number": "555-678-3542" | |
} | |
] | |
} | |
}, | |
{ | |
"class": "Person", | |
"name": "Steven Jobs", | |
"birthday": "Yesterday", | |
"nickname": "Steve", | |
"phonNumbers": { | |
"phonNumber": [ | |
{ | |
"class": "Phone", | |
"name": "cell", | |
"number": "555-123-4567" | |
}, | |
{ | |
"class": "Phone", | |
"name": "home", | |
"number": "555-987-6543" | |
}, | |
{ | |
"class": "Phone", | |
"name": "work", | |
"number": "555-678-3542" | |
} | |
] | |
} | |
} | |
] | |
} | |
}' http://localhost:8280/Test/xml/ | |
No comments:
Post a Comment