ESB WSO2 使用丰富、迭代和过滤中介器将属性添加到 XML 文件中

发布于 2025-01-13 06:27:02 字数 936 浏览 0 评论 0原文

我想将带有这样的 xml 文件的 Postman 请求发送到我的 api:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

我想向每个 添加一个 Yes/No 标签; 基于年龄。例如:如果年龄超过 23 岁,则 valid 标签的值应为 Yes。我想将修改后的消息发送到另一个 API。 我知道我应该使用丰富、迭代和过滤中介器,但我确实需要一些帮助,因为我是初学者。谢谢你!

I want to sent a Postman request with a xml file like this to my api:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

And I want to add a <valid>Yes/No</valid> tag to each <user> based on the age. For example: if the age is above 23, the value of the valid tag should be Yes. I want to send the modified message to another API.
I know that I should use Enrich, Iterate and Filter mediators but I really need some help because I'm a beginner. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-01-20 06:27:02

我使用了 Iterate 中介器,因为您需要后端调用和 filter 中介器用于设置每个元素的年龄是否有效,最后使用 enrich 中介器实现您的用例。

<!-- iteration of incoming user data -->
            <iterate expression="//data/user" id="iterateuser" sequential="true">
                <target>
                    <sequence>
                        <property expression="//age/text()" name="age" scope="default" type="STRING"/>
                         <log level="custom">
                <property name="Inside Iterator" value="is called*****"/>
                <property expression="get-property('age')" name="age"/>
            </log>
             <!-- fiter to find valid age  or not-->
                        <filter xpath="get-property('age')>='23'">
                            <then>
                                <property name="valid" scope="default" type="STRING" value="Yes"/>
                            </then>
                            <else>
                                <property name="valid" scope="default" type="STRING" value="No"/>
                            </else>
                        </filter>
                         <!-- enrich to add valid tag to existing payload-->
                        <enrich>
                            <source clone="true" type="inline">
                                <valid xmlns=""/>
                            </source>
                            <target action="child" xmlns:ns="http://www.wso2.org/types" xpath="//user"/>
                        </enrich>
                        <!-- enrich to add valid property value with validtag-->
                        <enrich>
                            <source clone="true" property="valid" type="property"/>
                            <target xpath="//valid"/>
                        </enrich>
                        <log level="full"/>
                        <!-- make an external endpoint call here -->
                    </sequence>
                </target>
            </iterate>

输入:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

迭代日志:

[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 22
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:8101fdcc-0a29-4bea-a8f1-b8ad80f0c6ff, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 26
[2022-03-15 12:27:23,040]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:b01f8b29-9fe5-43e6-a0ed-0a61d395fd1a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    <valid>Yes</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 20
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:1feeb723-8a72-475a-b014-cda2e7b7a90a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>

如果您注意到上面的日志 NoYes 已添加到有效负载中。

发布此内容后,您可以在迭代器内进行外部端点调用。

参考:
丰富中介

I have used Iterate mediator as you need backend call and filter mediator used to set age is valid or not for each element and finally used enrich mediator to achieve your usecase.

<!-- iteration of incoming user data -->
            <iterate expression="//data/user" id="iterateuser" sequential="true">
                <target>
                    <sequence>
                        <property expression="//age/text()" name="age" scope="default" type="STRING"/>
                         <log level="custom">
                <property name="Inside Iterator" value="is called*****"/>
                <property expression="get-property('age')" name="age"/>
            </log>
             <!-- fiter to find valid age  or not-->
                        <filter xpath="get-property('age')>='23'">
                            <then>
                                <property name="valid" scope="default" type="STRING" value="Yes"/>
                            </then>
                            <else>
                                <property name="valid" scope="default" type="STRING" value="No"/>
                            </else>
                        </filter>
                         <!-- enrich to add valid tag to existing payload-->
                        <enrich>
                            <source clone="true" type="inline">
                                <valid xmlns=""/>
                            </source>
                            <target action="child" xmlns:ns="http://www.wso2.org/types" xpath="//user"/>
                        </enrich>
                        <!-- enrich to add valid property value with validtag-->
                        <enrich>
                            <source clone="true" property="valid" type="property"/>
                            <target xpath="//valid"/>
                        </enrich>
                        <log level="full"/>
                        <!-- make an external endpoint call here -->
                    </sequence>
                </target>
            </iterate>

Input:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

Log for iteration:

[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 22
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:8101fdcc-0a29-4bea-a8f1-b8ad80f0c6ff, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 26
[2022-03-15 12:27:23,040]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:b01f8b29-9fe5-43e6-a0ed-0a61d395fd1a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    <valid>Yes</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 20
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:1feeb723-8a72-475a-b014-cda2e7b7a90a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>

If you noticed above log <valid>No</valid> or <valid>Yes</valid> is added to payload.

Post this, you can make external endpoint call inside iterator.

Ref:
Enrich Mediator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文