WSO2 ESB/WSO2 EI JSON请求后端服务 [英] WSO2 ESB / WSO2 EI JSON Request to backend service

查看:178
本文介绍了WSO2 ESB/WSO2 EI JSON请求后端服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用WSO2 EI将JSON消息发送到后端服务器.但是它发送了错误的json格式.我已将wso2ei-6.4.0用于此任务.我添加了我已经使用过的示例XML请求.

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>

但是我从ESB端收到了JSON请求.通过ESB服务发送邮件后.

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"
    }
}

此" roomOccupancyList "应具有JSONArray对象,默认情况下显示为JSONObject.如果我在此处添加了多个jsonElement,则显示为JSONArray.但是我什至需要一个jsonElement.您能帮我解决此问题吗?我已经参考了以上任务的以下链接. wso2文档链接

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响应应加载以下格式.

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"
        }
    ]
}

推荐答案

我已经使用了xslt来实现此功能,并且没有任何问题.另外,您需要在synapse.properties/ESB_HOME/conf文件夹中添加以下属性.

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

此示例XSLT供您进一步参考.

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>

这篇关于WSO2 ESB/WSO2 EI JSON请求后端服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆