Convert a JSON Message to SOAP and SOAP to JSON
The API Gateway has a default mediation flow for the API invocation requests that it receives. You can extend this default mediation flow to do additional custom mediation for the messages in the API Gateway. An extension is provided as a synapse mediation sequence. You can design sequences using a tool like WSO2 Developer Studio and then store the sequence in the Gateway's registry.
Let's see how to convert a message types using custom sequences. In this tutorial, we convert a JSON payload to SOAP before sending it to a SOAP backend. Then we receive the response in SOAP and convert it back to JSON.
This tutorial uses the PhoneVerification API that we created in Create and Publish an API.
Log in to the API Publisher and click the
PhoneVerificationAPI.
Click the Edit link next to the API's name to go to its edit mode.
Add the following resource to the API.
Tip: The resource you create here invokes the SOAP 1.2 Web service of the backend. Therefore, the recommended method is HTTP POST. As you do not include the payload in a query string, avoid giving any specific name in the URL pattern, which will be amended to the actual backend URL.
Field Sample value Resources URL pattern /* Request types POST
After the resource is added, expand it and add a parameter as follows. This parameter is used to pass the payload to the backend.
Parameter name Description Parameter Type Data Type body Pass the phone number and license key body String Next, let's write a sequence to convert the JSON payload to a SOAP request. We do this because the backend accepts SOAP requests.
Navigate to the Implement page and change the endpoint of the API to http://ws.cdyne.com/phoneverify/phoneverify.asmx?WSDL. Once the edits are done, click Save.
Download WSO2 Developer Studio (version 3.7.1 is used here) from http://wso2.com/products/developer-studio/ and open it by double clicking the
Eclipse.appfile inside the downloaded folder.Tip: To start Eclipse on a Mac for the first time, open a terminal and execute the following commands:
cd <DevStudio_Home>/Eclipse.app/Contents/MacOS/
chmod +x eclipse
./eclipseThereafter, you can start Eclipse by double-clicking the Eclipse icon in
<DevStudio_Home>.- Click the Developer Studio menu and choose Open Dashboard. When the dashboard opens, click the ESB Config Project link.
- Create a new ESB project by the name
PhoneProject.
Click the Sequence link on the Developer Studio Dashboard and create a new sequence by the name
JSONtoSOAPin thePhoneProject.
Your sequence now appears on the Developer Studio console. From under the Mediators section, drag and drop a PayloadFactory mediator to your sequence and give the following values to the mediator.
Tip: The PayloadFactory mediator transforms the content of your message. The
<args>elements define arguments that retrieve values at runtime by evaluating the provided expression against the SOAP body. You can configure the format of the request/response and map it to the arguments.For example, in the following configuration, the values for the format parameters
PhoneNumberandLicenseKeywill be assigned with values that are taken from the <args> elements (arguments,) in that particular order.For details on how you got this configuration, see PayloadFactory Mediator in the WSO2 ESB documentation.
Payload <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <CheckPhoneNumber xmlns="http://ws.cdyne.com/PhoneVerify/query"> <PhoneNumber>$1</PhoneNumber> <LicenseKey>$2</LicenseKey> </CheckPhoneNumber> </soap12:Body> </soap12:Envelope>Args Give the following arguments:
Type Value Evaluator expression //request/PhoneNumberxml expression //request/LicenseKeyxml Similarly, add a property mediator to the same sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/soap+xml Property Scope axis2 Save the sequence, which is in XML format (e.g.,
JSONtoSOAP.xml). This will be theInsequence for your API.Go back to the Developer Studio Dashboard, click the Sequence link and create another sequence by the name
SOAPtoJSONin thePhoneProject.
Add a property mediator to the second sequence and give the following values to the property mediator.
Property Name messageType Value Type Literal Value application/json Property Scope axis2 Save the sequence, which is in XML format (e.g.,
SOAPtoJSON.xml). This will be theOutsequence for your API.- Log in to the API Gateway's management console. If you are using WSO2 Cloud, the Gateway URL is
https://gateway.api.cloud.wso2.com/carbon/admin/login.jsp. If you are using a local setup, the URL ishttps://localhost:9443/carbon. You can see the username on the top right-hand corner of the API Publisher. After logging in, click the Browse menu under the Resources menu.
- When the API Gateway's registry opens, navigate to the registry path
/_system/governance/apimgt/customsequences/in. This is because you want the custom sequence to be invoked in theIndirection or the request path.
Click Add Resource and upload the XML file of the sequence that you created earlier.
Next, let's write another sequence to convert the SOAP response that the backend sends to JSON.- Similarly, navigate to the registry path
/_system/governance/apimgt/customsequences/outand upload theSOAPtoJSON.xmlsequence file. This will invoke the second sequence in theOutdirection or the response path.
- Log back to the API Publisher, click the Edit link associated wit the API, navigate to the Manage tab, click the Sequences check-box and engage the
Inandoutsequences that you created earlier.
- Save the API.
You have created an API, a resource to access the SOAP backend and engaged sequences to the request and response paths to convert the message format from JSON to SOAP and back to JSON. Let's subscribe to the API and invoke it. Log in to the API Store and subscribe to the API and create an access token if you have not done so already.
Go to the API Console tab and expand the POST method.
Give the payload in the
bodyparameter in JSON format and click Try it out. Here's a sample JSON payload: {"request":{"PhoneNumber":"18006785432","LicenseKey":"0"}}
Note that you get a JSON response to the JSON request whereas the backend accepts SOAP messages. The request and response are conve rted by the sequences that you engaged at the API Gateway.
In this tutorial, you converted a message from JSON to SOAP and back to JSON using In and Out sequences.







