WSO2 ESB 4.5.1: SOAP to JSON transformation issues - json

So I´m trying to build a proxy based on a wsdl. This proxy will listen on hhtp/https and transform the incoming SOAP message into JSON sending it over JMS to the end-point. And of course the way back that is from JSON to SOAP needs to be considered as well.
Can somebody please help me how to do that. I´ve tried sample 440 on ESB 4.5.1 just switching the "in_transform" with "out_transform", also some other recipes found here in stackoverflow but none of them seems to do the job. In all cases I get an empty JMS message at the end-point.
Here´s a copy of my proxy
<proxy xmlns="http://ws.apache.org/ns/synapse" name="Tarjetas" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<property name="messageType" value="application/json" scope="axis2"/>
</inSequence>
<outSequence>
<log level="full"/>
<xslt key="out_transform"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
<endpoint>
<address uri="jms:/FORTALEZA.BRIDGE.MOBILE.QUEUE?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.DestinationType=queue"/>
</endpoint>
</target>
<publishWSDL uri="file:repository/wsdl/Debito.wsdl"/>
<description></description>
</proxy>
and here´s my localEntry for out_transform
<localEntry key="out_transform">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:m0="http://services.samples"
version="2.0"
exclude-result-prefixes="m0 fn">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://services.samples">
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
</localEntry>
and I´m using the following within axis2.xml
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/>
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONStreamBuilder"/>
I really appreciate all the help I can get.

You need to use the following message formatter and message builder in the axis2.xml
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONBuilder"/>
You can use the StreamFormatter when you are building the JSON message using a script mediator. Otherwise the above formatter/builder would work for your requirement. You need to comment out the StreamFormatters which you have uncommented.

Related

Spring 4.0.5 -> 4.2.2 XML no longer valid - Endpoint no longer returning JSON

Scenario: Using gradle to update from Spring 4.0.5.RELEASE to 4.2.2.RELEASE causes an endpoint to return XML instead of JSON.
I have an endpoint on my server that returns a list of objects. Previously, I could hit this endpoint in my browser and view the JSON object in a nice format due to my JSONView extension. After upgrading Spring, however, that same endpoint now displays XML in the browser. However, when using js to make a request to the same endpoint, I am given JSON.
Is there something I need to change to revert to the old behavior while still maintaining the 4.2.2 release?
It seems to be stemming from this issue, which Spring's reaction to changed between the two versions: https://github.com/strongloop/strong-remoting/issues/118.
The following is my spring-api-servlet.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.XXX.spring" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
</bean>
Not sure of the details but this is probably a content type negotiation thing. spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc has a high level description. In spring increments, spring will sometimes fiddle with or re-order message converters, meaning the one that "wins" is non-specific if the request accept header is broad.

How to convert SOAP response with xsi values to json in WSO2esb

I'm using wso2 esb 4.8.1 version for SOAP to rest conversion with as API. I have a soap request with xsi values. After I generate the proper soap request with script mediator and um getting an expected response. But I have an issue with response. Because I need t convert the soap response into json. When I tried with following out sequence um not getting the proper json response with axis2. How can I convert this soap response into json properly?
This it the soap response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:easyDownloadResponse xmlns:ns1="http://usermanage.ivas.huawei.com" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<easyDownloadReturn href="#id0"/>
</ns1:easyDownloadResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://response.usermanage.ivas.huawei.com" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:EasyDownloadResp">
<eventClassName xsi:type="xsd:string" xsi:nil="true"/>
<failedResources xsi:type="soapenc:Array" xsi:nil="true"/>
<operationID xsi:type="xsd:long">0</operationID>
<resultCode xsi:type="xsd:int">0</resultCode>
<resultInfo xsi:type="xsd:string" xsi:nil="true"/>
<returnCode xsi:type="xsd:string">000000</returnCode>
<toneTransactionID xsi:type="soapenc:Array" xsi:nil="true"/>
<transactionID xsi:type="xsd:string" xsi:nil="true"/>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
This is the response which i'm getting
{"easyDownloadResponse":{"#encodingStyle":"http://schemas.xmlsoap.org/soap/encoding/","easyDownloadReturn":{"#href":"#id0"}}}
This is my out sequence
<outSequence xmlns="http://ws.apache.org/ns/synapse">
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
All your answers are highly welcome.
Finally I found the solution for the issue. In the normal case we use
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
But this axis2 property cannot convert the complex soap response into json such as with xsi.
For that need to use the following axis2 property. Then it converts the entire soap response into Json as we expected.
<property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
This is my full outsequence.
<outSequence xmlns="http://ws.apache.org/ns/synapse">
<property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
You can try payload mediator to get exact json format 1. But still you may have to uncomment the following lines in
$ESB_HOME/repository/conf/axis2/axis2.xml
<!--messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/-->
<!--messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONStreamBuilder"/-->
By default, JSON messages are converted to XML when they are received by the PayloadFactor mediator. However, if you enable the JSON stream formatter and builder, incoming JSON messages are left in JSON format.
And also you can use script mediator again (In outsequence) to modify your json response. Refer this sample

JSON to SOAP WSO2 Api manager xml sequence

I would to publish a SOAP service as REST(json) API . The service operation I want to expose has a xml sequence element in the WSDL/XSD definition:
<xs:complexType name="hellolist">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="hellolistResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
I don't know how to map a JSON array to a xml sequence using PayloadFactory. All the samples I found deal only with simple json and SOAP messages like this sample WSO2 transformation.
I would like to transform this json message:
{"hellolist":{"name":["Peter","Mary","Ann","James"]}}
To this soap message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dum="http://dummyservice2.com/">
<soapenv:Header/>
<soapenv:Body>
<dum:hellolist>
<name>Peter</name>
<name>Mary</name>
<name>Ann</name>
<name>James</name>
</dum:hellolist>
</soapenv:Body>
</soapenv:Envelope>
You can use payload factory mediator to do this. It can be used to transform or replace message content in between the client and the back-end server. In the case of your scenario you can configure a proxy service as below.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="json_to_xml_factory"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<payloadFactory media-type="xml">
<format>
<dum:hellolist xmlns:dum="http://dummyservice2.com/">
<name xmlns="">$1</name>
<name xmlns="">$2</name>
<name xmlns="">$3</name>
<name xmlns="">$4</name>
</dum:hellolist>
</format>
<args>
<arg evaluator="json" expression="$.hellolist.name[0]"/>
<arg evaluator="json" expression="$.hellolist.name[1]"/>
<arg evaluator="json" expression="$.hellolist.name[2]"/>
<arg evaluator="json" expression="$.hellolist.name[3]"/>
</args>
</payloadFactory>
<log level="full"/>
</inSequence>
<outSequence/>
</target>
<description/>
</proxy>
Please refer below links for more information on this.
https://docs.wso2.com/display/ESB490/PayloadFactory+Mediator
http://christinetechtips.blogspot.com/2014/02/payload-factory-mediators-to-work-with.html
http://madhukaudantha.blogspot.com/2013/05/wso2-esb-payload-mediator-tutorial.html
If you have a non-static payload (your case), payload-factory-mediator will not be the solution (only for static payload). The best way for your kind of a problem is using xslt mediator + enrich mediator in WSO2 ESB. You can try out this example, https://docs.wso2.com/display/ESB481/Sample+440%3A+Converting+JSON+to+XML+Using+XSLT
Or you can use script mediator in case, as explained in this example.https://docs.wso2.com/display/ESB481/Sample+350%3A+Introduction+to+the+Script+Mediator+Using+JavaScript . Yet not the most preffered way.
I have done your kind of conversions using WSO2 ESB as I described above. But I don't know whether those mediator tools are also available with WSO2 APIM (I couldn't find any example when I googled for your problem).
Try the steps mention below
Steps to Convert a SOAP Test service in SoapUI with assertions into JSON service with assertions
Copy the project xml file of SOAP UI project
Open the copied xml file
Find the format for all assertion types in JSON that is equal in SOAP call assertion
Now convert all the soap assertions in the config node of the test step node to JSON assertion config node format as found in the previous step
Convert the SOAP call request into JSON call by changing all the fields as below
• Add the output_format node
• Add the Rest call – service name
• Remove the nodes without value and convert the others into JSON format like {“node name” : “value”} etc.,
In the test step node type attribute – it will be “request” change it to “httprequest”
Save the file and open it in SOAP UI
Now you can see all the services in the newly opened project will be in JSON format and all assertions will be available that were in SOAP call previously.
also refer the link which helps to convert the request completely including the assertions
please reply if it is not helpful

How can we convert from <a>fai</a><b>3</b> to <fai>3</fai> using xpath in WSO2 ESB

I am getting JSON like this
{"request":""AttributesCriteriaList":[
{"FieldName":"PartyBranchId","OperationType":1,"FieldValue":"-1500000000","JoinType":2},
{"FieldName":"AssetTypeId","OperationType":1,"FieldValue":"-1500000001","JoinType":2},
{"FieldName":"PeriodFrom","OperationType":10,"FieldValue":"1356998400","JoinType":2},
{"FieldName":"PeriodTo","OperationType":11,"FieldValue":"1358208000","JoinType":2},
{"FieldName":"Status","OperationType":1,"FieldValue":"4","JoinType":2}
]
}
How can I convert in to
{"assetid":"150000001","partybranchid":"152555222"}
in WSO2 ESB?
The easiest way is to translate the given message using XSLT (If you aware of writing XSLT this seems pretty simple)
Then you just required to write a a proxy (in WSO2 ESB),
<proxy name="JSONTOJSONTranformProxy" transports="https,http">
<target>
<inSequence>
<xslt key="translate.xslt"/>
<property name="messageType" value="application/json" scope="axis2"/>
<send>
<address ="[SOME_ADDRESS]"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>

How to convert the JSON in WSO2 ESB

I am trying to pass my JSON as a object to a WSO2 ESB.
I am getting JSON like this:
'{FIELDNAME":"NAME",FIELDVALUE:"KISHORE"}'
This JSON I need to pass my ESB as a dynamic column. But Its accepting like JSON {"NAME":"KISHORE"}. How can I convert from above JSON to below one in WSO2 ESB. If I get above one I am unable to pass to proxy. If I get below one I will pass like this (//name/child::text()) then I will get value as "KISHORE" I was tried with ENRICH mediator but its not working.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="test_dynamic" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="RM"/>
</enrich>
<property name="RM" expression="//fieldname/child::text()" scope="default" type="STRING"/>
<log level="custom">
<property name="r_no" expression="get-property('R_no')"/>
<property name="r_value" expression="get-property('R_value')"/>
<property name="emp_d" expression="get-property('emp')"/>
<property name="RM" expression="get-property('RM')"/>
</log>
<log level="full"/>
</inSequence>
</target>
<description></description>
</proxy>
ANS: request, Envelope:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body><fieldname>e_no</fieldname><fieldvalue>5</fieldvalue></soapenv:Body>
</soapenv:Envelope>
I think you should send the JSON message like this:
'{"RECORD":{FIELDNAME":"NAME",FIELDVALUE:"KISHORE"}}'
then get the value by the "Property" mediator in WSO2 ESB, like
<property name="FIELDNAME" expression="//FIELDNAME/text()" scope="default" type="STRING"/>
<property name="FIELDVALUE" expression="//FIELDVALUE/text()" scope="default" type="STRING"/>
The link provided below by WSO2 explains the exact usecase which you are interested in solving
Steps:
Convert JSON to XML using the XSLT mediator
Use the Enrich mediator to replace the Body of the SOAP message
Follow this link for detailed info and actual proxy configuration
https://docs.wso2.com/display/ESB480/Sample+440%3A+Converting+JSON+to+XML+Using+XSLT