How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

StackOverflow https://stackoverflow.com/questions/22533679

  •  18-06-2023
  •  | 
  •  

سؤال

i am new to wso2.. My requirement is

1) I want to transform incoming JSON request into XML format

2) Send that XML request to an external SOAP service

3) Response will get as XML and need to convert it into JSON format

Did somebody ever did that? If so, could you please share how you did?

I have the below configuration for a PROXY service

`<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="CelsiusToFahrenheitService"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="messageType" value="text/xml" scope="axis2"/>
         <property name="Proxy-Authorization"
                   expression="fn:concat('Basic', base64Encode('INDIA\username:pwd'))"
                   scope="transport"/>
         <property name="POST_TO_URI" value="true" scope="axis2"/>
         <property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
         <header name="Action"
                 value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
         <send>
            <endpoint>
               <address uri="http://www.w3schools.com/webservices/tempconvert.asmx"
                        format="soap11"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="messageType" value="text/xml" scope="axis2"/>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>`

XML Request

<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"> <Celsius>20</Celsius> </CelsiusToFahrenheit>

XMl Response

<CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"> <CelsiusToFahrenheitResult>68</CelsiusToFahrenheitResult> </CelsiusToFahrenheitResponse>

Need to send REQUEST as JSON and RESPOSNE also get as JSON

Can anyone please help me the scenario

How to use ScriptMediator to do the above proxy service. I have got sample from here (https://docs.wso2.org/display/ESB481/Sample+441%3A+Converting+JSON+to+XML+Using+JavaScript)

I have did my configuration like this (Don't know is it correct or not)

<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="JsonToXMLProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence> <script language="js">var cel= mc.getPayloadJSON().CelsiusToFahrenheit.Celsius.toString(); mc.setPayloadXML( &lt;CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"&gt; &lt;Celsius&gt;{cel}&lt;/Celsius&gt; &lt;/CelsiusToFahrenheit&gt;);</script> <property name="messageType" value="text/xml" scope="axis2"/> <log level="full"/> </inSequence> <outSequence> <log level="full"/> <property name="messageType" value="text/xml" scope="axis2"/> <send/> </outSequence> <endpoint> <address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/> </endpoint> </target> <description/> </proxy>

But i am getting the exception like this

`[2014-03-20 18:19:02,391]  INFO - LogMediator To: /services/JsonToXMLProxy.JsonToXMLProxyHttpEndpoint, MessageID: urn:uuid:a2eeeb26-94e1-4ed1-a3f9-79f1d1461821, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"><Celsius>20</Celsius></CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>
[2014-03-20 18:19:03,130]  WARN - ClientHandler Received an unexpected response - of content type : text/html and status code : 411 with reason : Length Required For : 172.26.40.214:8080 For Request : Axis2Request [Message ID : urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d] [Status Completed : true] [Status SendingCompleted : true]
[2014-03-20 18:19:04,133]  INFO - BuilderUtil OMException in getSOAPBuilder
org.apache.axiom.om.OMException: SOAP message MUST NOT contain a Document Type Declaration(DTD)
        at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createDTD(StAXSOAPModelBuilder.java:462)
        at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:282)
        at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:204)
        at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:154)
        at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:140)
        at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(BuilderUtil.java:659)
        at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:198)
        at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)
        at org.apache.synapse.transport.nhttp.ClientWorker.run(ClientWorker.java:253)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
[2014-03-20 18:19:04,137]  INFO - BuilderUtil Remaining input stream :[]
[2014-03-20 18:19:04,137]  WARN - ClientWorker Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)
[2014-03-20 18:19:04,153]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d, Direction: response, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>411</faultcode><faultstring>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</faultstring><detail>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>`

Please help me for this issue

Thanks in advance.

هل كانت مفيدة؟

المحلول

you could use this example using de payloadfactory mediator to build the messages, it work for me in your scenario:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="JsonToXMLProxy"
       transports="https http local"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <endpoint>
         <address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/>
      </endpoint>
      <inSequence>
         <log>
            <property name="TEMPERATURA_ENTRADA" expression="json-eval($.celsius)"/>
         </log>
         <payloadFactory media-type="xml">
            <format>
               <web:CelsiusToFahrenheit xmlns:web="http://www.w3schools.com/webservices/">
                  <web:Celsius>$1</web:Celsius>
               </web:CelsiusToFahrenheit>
            </format>
            <args>
               <arg evaluator="json" expression="$.celsius"/>
            </args>
         </payloadFactory>
         <header name="Action"
                 value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
      </inSequence>
      <outSequence>
         <log>
            <property xmlns:p="http://www.w3schools.com/webservices/"
                      name="TEMPERATURA_SALIDA"
                      expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
         </log>
         <payloadFactory media-type="json">
            <format>
                            "Temperatura" : {
                                "EnFahrenheit" : $1
                            }
            </format>
            <args>
               <arg xmlns:p="http://www.w3schools.com/webservices/"
                    evaluator="xml"
                    expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
            </args>
         </payloadFactory>
         <property name="messageType" value="application/json" scope="axis2"/>
         <send/>
      </outSequence>
   </target>
</proxy>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top