簡體   English   中英

WSO2 ESB JSON到SOAP

[英]WSO2 ESB JSON to SOAP

大多數當前文檔都參考了SOAP-to-JSON,我希望在使用WSO2 ESB將JSON響應對象轉換為SOAP服務時是否有任何參考資料或教程。 提前致謝。

樣品服務: http//api.statsfc.com/premier-league/table.json?key = free

您可以使用類似於以下的配置來實現此目的; (我們必須將“messageType”屬性設置為“text / xml”,以便在響應客戶端時使用SOAP消息構建器。)

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

但是,如果您的JSON響應對象與您從提供的示例服務獲得的對象完全相同(即,如果它是匿名對象的數組),則ESB將減少響應以僅包括第一個元素(參見下面的SOAP響應)。

<?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>                    

我可以使用ESB 4.5.0中的以下步驟轉換完整的JSON有效負載。 這些步驟涉及更改application / json內容類型的消息構建器和消息格式化程序。

更改消息構建器,格式化JSON; 在CARBON_HOME / repository / conf / axis2 / axis2.xml文件中,通過注釋掉以下行來取消默認消息構建器和消息格式化程序,

<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONBuilder"/>
<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>

通過取消注釋以下行來啟用JSONStreamBuilder和JSONStreamFormatter,

<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONStreamFormatter"/>
<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONStreamBuilder"/>

編寫一個Javascript函數來轉換和構建新的XML有效負載。

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);
}

修改outSequence以對傳入的JSON有效負載執行轉換。

<outSequence>
    <script language="js" key="conf:/repository/esb/scripts/transformrequest.js" function="transformRequest"/>
    <property name="messageType" value="text/xml" scope="axis2"/>
    <send/>
</outSequence>

AFAIU您想要使用json內容調用soap服務並獲取json響應。 如果這是您的要求, 示例將為您提供幫助。

如果您希望允許SOAP客戶端通過WSO2ESB訪問REST服務,則可以。 看看這個示例: http//docs.wso2.org/wiki/display/ESB451/Sample+152%3A+Switching+Transports+and+Message+Format+from+SOAP+to+REST+POX

您可以做的是創建一個位於REST服務前面的SOAP代理服務。 然后,此代理服務將接收SOAP請求,將請求轉換為REST請求並轉發到REST服務。 然后,它可以將JSON中的REST響應轉換為SOAP響應並返回到SOAP客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM