WSO2 ESB JSON to SOAP
Asked Answered
W

5

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

Wellknit answered 2/1, 2013 at 19:15 Comment(0)
T
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>                    
Taffeta answered 3/1, 2013 at 16:44 Comment(2)
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?Wellknit
Mohamed, I suggested a new approach to get your requirement done.Taffeta
T
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>
Taffeta answered 9/1, 2013 at 17:53 Comment(0)
P
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.

Poirier answered 3/1, 2013 at 4:56 Comment(1)
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.Wellknit
C
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.

Crittenden answered 3/1, 2013 at 11:40 Comment(0)
P
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.

Passport answered 24/10, 2022 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.