简体   繁体   English

WSO2-如何使用脚本介体将此XML转换为Json?

[英]WSO2 - How to convert this XML to Json using script mediator?

Im using WSO2 ESB and DDS to make an API to read data from database and finally I got this respone. 我使用WSO2 ESB和DDS制作了一个API以从数据库读取数据,最后我得到了响应。 How to convert this XML to Json using script mediator? 如何使用脚本介体将此XML转换为Json? Give me an example please. 请给我一个例子。

<?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
    <employeeCollection xmlns="http://employee.us.wso2.com">
        <employee>
            <EmployeeID>17</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>18</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>19</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>

    </employeeCollection>
</soapenv:Body>

And this is the configuration 这是配置

<resource methods="POST" uri-template="/team">
  <inSequence>
     <sequence key="conf:/SendSelectWithTeam"/>
     <call>
        <endpoint>
           <address uri="https://192.168.2.165:9453/services/EmployeesDataService/" format="soap12"/>
        </endpoint>
     </call>

     <respond/>
  </inSequence>

Additional question: How to get the value of the each of "EmployeeID" using script mediator? 附加问题:如何使用脚本中介程序获取每个“ EmployeeID”的值?

If you just want to convert this directly to json, you don't need to do it manually using script mediator. 如果您只是想直接将其转换为json,则无需使用脚本中介程序手动进行。

You can update the out sequence of your ESB proxy (or API) like this and it will convert the response xml to json. 您可以像这样更新ESB代理(或API)的输出顺序,它将响应xml转换为json。

<outSequence>
     <property name="messageType" value="application/json" scope="axis2"/>
     <send/>
</outSequence>

See WSO2 Docs for more details. 有关更多详细信息,请参见WSO2文档

Edit: 编辑:

In your case, if you use <respond> mediator, it will bypass out sequence, and my suggestion won't work. 在您的情况下,如果您使用<respond>介体,它将绕过序列,而我的建议将不起作用。 You have 2 options to get it working. 您有2种选择可以使其正常运行。

1) Use Send mediator instead of call and respond mediators. 1)使用“发送调解器”代替“ callrespond调解器。

or 要么

2) Use loopback mediator instead of respond mediator. 2)使用loopback介体代替respond介体。

Refer this link 引用此链接

How to convert SOAP response with xsi values to json in WSO2esb 如何在WSO2esb中将具有xsi值的SOAP响应转换为json

Please change the proxy outSequence settings in config file like below: 请更改配置文件中的proxy outSequence设置,如下所示:

  <outSequence xmlns="http://ws.apache.org/ns/synapse">
   <property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
   <send></send>
</outSequence>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM