WSO2 ESB / WSO2 EI JSON Request to backend service

2019-08-19 09:51发布

问题:

I have sent JSON Message to backend server using WSO2 EI. But it sending wrong json format. I have used wso2ei-6.4.0 for this task. I have added sample XML request which I have used.

<jsonObject>
   <checkInDate>2019-03-25</checkInDate>
   <checkOutDate>2019-03-26</checkOutDate>
   <cityCode>3086</cityCode>
   <hotelCode />
   <roomOccupancyList>
      <jsonArray>
         <?xml-multiple jsonElement?>
         <jsonElement>
            <childAges>0</childAges>
            <numberOfAdults>1</numberOfAdults>
            <numberOfChildren>0</numberOfChildren>
            <roomNo>1</roomNo>
         </jsonElement>         
      </jsonArray>
   </roomOccupancyList>
</jsonObject>

But I got following JSON request from ESB side. Once send the message through the ESB service.

{
    "checkInDate": "2019-06-04", 
    "checkOutDate": "2019-06-05", 
    "cityCode": "1344", 
    "hotelCode": "", 
    "roomOccupancyList": {
        "childAges": "0", 
        "numberOfAdults": "1", 
        "numberOfChildren": "0", 
        "roomNo": "1"
    }
}

This "roomOccupancyList" should have JSONArray Object, by default it showing as JSONObject. If I have add multiple jsonElement here, it showing as JSONArray. But I need even one jsonElement also. Can you please help me to fixed this issue. I have refereed following link for above task. wso2 doc link

JSON Response should load following format.

{
    "checkInDate": "2019-06-04", 
    "checkOutDate": "2019-06-05", 
    "cityCode": "1344", 
    "hotelCode": "", 
    "roomOccupancyList": [
        {
            "childAges": "0", 
            "numberOfAdults": "1", 
            "numberOfChildren": "0", 
            "roomNo": "1"
        }
    ]
}

回答1:

I have used following xslt for this and it is working without any issue. Also you need to add following property in synapse.properties /ESB_HOME/conf folder.

synapse.json.to.xml.processing.instruction.enabled=true

This the sample XSLT for your further referance.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*"/>
       <xsl:output indent="yes"/>    

    <xsl:template match="/request">
        <availabilityRequest>       
            <jsonObject>           
               <checkInDate>2019-03-25</checkInDate>
               <checkOutDate>2019-03-26</checkOutDate>
               <cityCode>3086</cityCode>    
               <xsl:variable name="roomCount" select="count(roomsInformation)" />

               <xsl:for-each select="roomsInformation">     
                   <xsl:if test="$roomCount = 1">
                        <xsl:processing-instruction name="xml-multiple"/>
                   </xsl:if>
                   <xsl:variable name="roomIndex" select="position()" /> 
                   <roomOccupancyList>
                        <jsonArray>
                            <childAges>0</childAges>
                                <numberOfAdults>1</numberOfAdults>
                                <numberOfChildren>1</numberOfChildren>
                                <roomNo><xsl:value-of select="$roomIndex" /></roomNo>                                                                              
                        </jsonArray>                                                          
                   </roomOccupancyList> 
                   <xsl:if test="$roomCount = 1">
                        <xsl:copy-of select="Objects"></xsl:copy-of>
                   </xsl:if>                   
               </xsl:for-each>                         
            </jsonObject>   
        </availabilityRequest>          
    </xsl:template>
</xsl:stylesheet>