6

Most of the current docs are with reference to SOAP-to-JSON, I was hoping whether there are any reference material or tutorials when using WSO2 ESB to transform JSON response object to SOAP service. Thanks in advance.

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

Community
  • 1
  • 1
Mohamed Suhail
  • 169
  • 3
  • 9

5 Answers5

6

You can achieve this with a configuration similar to the following; (We must set the "messageType" property to "text/xml" to engage 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 same as the one you get from the sample service that you've provided (ie., if it is an array of anonymous objects), the ESB is going to cut down the response to include only the first element (see following SOAP response).

<?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>                    
udeshike
  • 388
  • 4
  • 9
  • Thank you udeshike, the above was quite helpful. Unfortunately the JSON response is very similar to the above and the ESB does only includes the first element in the SOAP response, any possible work around for this issue? – Mohamed Suhail Jan 06 '13 at 17:03
  • Mohamed, I suggested a new approach to get your requirement done. – udeshike Jan 09 '13 at 17:57
4

I could transform the complete JSON payload with following steps in ESB 4.5.0. These steps involve changing the message builder and message formatter for the application/json content type.

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

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

Engage JSONStreamBuilder and JSONStreamFormatter by uncommenting 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 transform 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 the outSequence to execute the transformation 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>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
udeshike
  • 388
  • 4
  • 9
2

AFAIU you want to invoke a soap service with a json content and get a json response. If that is your requirement, this sample will help you.

Amila Maharachchi
  • 2,121
  • 2
  • 15
  • 21
  • Thanks Amila for responding, but unfortunately this is not my requirement. The sample service link i shared is a rest service which sends a JSON response after invoking it. What I would like to know is, whether it is possible to transform this JSON response to a SOAP response. – Mohamed Suhail Jan 03 '13 at 10:52
1

If you want to allow SOAP clients to access a REST service through the WSO2ESB, it 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 sits in front of your REST service. This proxy service will then take in SOAP requests, convert the request into a REST request and forward to the REST service. It can then transform the REST response in JSON into a SOAP response and return to the SOAP client.

RaviU
  • 1,203
  • 15
  • 17
0

The most current documentation for APIM 4.1.0, and thus Micro Integrator, demos two different ways to handle this business use case of JSON to SOAP output:

  • Use the PayloadFactory Mediator
  • Use XSLT Mediator

While the message Formatter/Builder approach listed in an another answer here will work in a very generic way, and thus usable if you have a one-to-one field output, it doesn't give you the flexibility that these two approaches will give you for field transformations.

Scott
  • 46
  • 5