WSO2 ESB JSON for SOAP

Most of the current documents are SOAP-to-JSON, I was hoping there would be some reference material or tutorials when using the WSO2 ESB to convert a JSON response object to a SOAP service. Thanks in advance.

Service example: http://api.statsfc.com/premier-league/table.json?key=free

+6
source share
4 answers

You can achieve this with a configuration similar to the following: (We must set the messageType property to "text / xml" to enable the SOAP message builder when responding back to the client.)

<proxy xmlns="http://ws.apache.org/ns/synapse" name="JSONToSOAPService" transports="https,http"> <target> <outSequence> <log level="full"/> <property name="messageType" value="text/xml" scope="axis2"/> <send/> </outSequence> <endpoint> <address uri="http://api.statsfc.com/premier-league/table.json?key=free"/> </endpoint> </target> <description></description> </proxy> 

But if your JSON response object is exactly the same as the one you received from the sample service you provided (that is, if it is an array of anonymous objects), the ESB is going to shorten the response to include only the first element (see the following SOAP answer).

 <?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body> <position>1</position> <team_id>10260</team_id> <team>Manchester United</team> <played>21</played> <won>17</won> <drawn>1</drawn> <lost>3</lost> <for>54</for> <against>28</against> <difference>26</difference> <points>52</points> <info>championsleague</info> </soapenv:Body> </soapenv:Envelope> 
+6
source

I can convert all JSON payload with the following steps in ESB 4.5.0. These steps include modifying the message builder and message formatting for the application / json content type.

Change the message builder, formatter for JSON; In CARBON_HOME / repository / conf / axis2 / axis2.xml, disable the default message builder and message formatter by commenting out the following lines,

 <messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONBuilder"/> <messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/> 

Include JSONStreamBuilder and JSONStreamFormatter, uncommenting the following lines,

 <messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONStreamFormatter"/> <messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONStreamBuilder"/> 

Write a Javascript function to convert and build the new XML payload.

 function transformRequest(mc) { var array = mc.getPayloadJSON(); var payload = <games/>; for (i = 0; i < array.length; ++i) { var elem = array[i]; payload.game += <game> <position>{elem.position}</position> <team_id>{elem.team_id}</team_id> <team>{elem.team}</team> <played>{elem.played}</played> <won>{elem.won}</won> <drawn>{elem.drawn}</drawn> <lost>{elem.lost}</lost> <for>{elem["for"]}</for> <against>{elem.against}</against> <difference>{elem.difference}</difference> <points>{elem.points}</points> <info>{elem.info}</info> </game> } mc.setPayloadXML(payload); } 

Modify outSequence to perform the conversion on the incoming JSON payload.

 <outSequence> <script language="js" key="conf:/repository/esb/scripts/transformrequest.js" function="transformRequest"/> <property name="messageType" value="text/xml" scope="axis2"/> <send/> </outSequence> 
+4
source

AFAIU you want to call the soap service using json content and get a json response. If this is your requirement, this sample will help you.

+1
source

If you want to allow SOAP clients to access the REST service through WSO2ESB, this is possible. Take a look at this sample: http://docs.wso2.org/wiki/display/ESB451/Sample+152%3A+Switching+Transports+and+Message+Format+from+SOAP+to+REST+POX

What you can do is create a SOAP proxy service that is in front of your REST service. This proxy service will then accept SOAP requests, convert the request to a REST request, and redirect it to the REST service. It can then convert the REST response to JSON into a SOAP response and return to the SOAP client.

+1
source

All Articles