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