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.
"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,