Powered By Blogger

Dec 29, 2014

XML to JSON conversion using a proxy in WSO2 ESB

This post describes about the way to convert an XML payload into JSON. Basically this can be achieved by using the property "messageType" in synapse configuration.

The property is as follows.

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

I have created a proxy called TestProxy with the  SimpleStockQuote endpoint as follows.

 <?xml version="1.0" encoding="UTF-8"?>  

 <proxy xmlns="http://ws.apache.org/ns/synapse"  

 name="TestProxy"  

 transports="https,http"  

 statistics="disable"  

 trace="disable"  

 startOnLoad="true">  

 <target>  

 <inSequence>  

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

 <log level="full"/>  

 <send>

 <endpoint>  

 <address uri="http://localhost:9000/services/SimpleStockQuoteService/"/>  

 </endpoint>  

</send>
 </inSequence>  

 <outSequence>  

 <send/>  

 </outSequence> 

 </target>  

 <description/>  

 </proxy>   



In order to invoke this proxy you can follow below steps.

1. Build the SimpleStockQuote Service at <ESB_HOME>/samples/axis2Server/src/SimpleStockQuoteService by running "ant" command

2. Then start sample backend to recover the response which is provided with WSO2 ESB by navigating to <ESB_HOME>/samples/axis2Server and enter the command "sh axis2Server.sh"

3. Send the below SOAP request to proxy service using SOAP UI.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m0="http://services.samples" xmlns:xsd="http://services.samples/xsd">  

 <soapenv:Header/>  

 <soapenv:Body>  

 <m0:getQuote xmlns:m0="http://services.samples" id="12345">  

 <person>Anvar</person>  

 <person>Porter</person>  

 <person>Raj</person>  

 <m0:request>  

 <m0:symbol>A</m0:symbol>  

 </m0:request>  

 </m0:getQuote>  

 </soapenv:Body>  

 </soapenv:Envelope>  

Response should be as follows:

 HTTP/1.1 200 OK  

 Host: sohani-ThinkPad-T530:8280  

 SOAPAction: "urn:getQuote"  

 Accept-Encoding: gzip,deflate  

 Content-Type: application/json  

 Date: Fri, 29 Dec 2014 09:00:00 GMT  

 Server: WSO2-PassThrough-HTTP  

 Transfer-Encoding: chunked  

 Connection: Keep-Alive  

 {"getQuote":{"@id":"12345","person":{"@id":"12345","@sex":"female","@target":"urn:getQuote","firstname":"Anna","lastname":{"@id":"12345678","$":"Smith"}},"request":{"symbol":"A"}}}  

No comments:

Post a Comment