【问题标题】:WSO2 EI/ESB: Implementing Switch mediator and Filter mediator using Regular ExpressionWSO2 EI/ESB:使用正则表达式实现 Switch mediator 和 Filter mediator
【发布时间】:2020-05-13 13:26:05
【问题描述】:

在我的 API 中,我有一个类似的属性:

<property expression="json-eval($.Entity.users.name)" name="uri.var.name"/>

我想使用 Switch 中介和 Filter 中介根据上述属性路由到不同的后端。

例如,如果属性可以有 4 个不同的值:Nick、Tom、Jade、Dave

  1. 如果属性名称为 Nick 或 Jade,它将指向 back-end-1。

  2. 如果属性名称为 Tom 或 Dave,它将指向 back-end-2。

    <switch source="json-eval(uri.var.name)">
       <case regex="Nick|Jade">
          <send>
             <endpoint>
                <http method="get" uri-template="https://backend1.com" />
             </endpoint>
          </send>
       </case>
       <case regex="Tom|Dave">
          <send>
             <endpoint>
                <http method="get" uri-template="https://backend2.com" />
             </endpoint>
          </send>
       </case>
       <default />
    </switch>

这不起作用。 在 Switch 中介器中定义 Source 和 Regex 的正确方法是什么?

Filter mediator 中也是如此

【问题讨论】:

    标签: wso2 wso2esb esb wso2ei ei


    【解决方案1】:

    您在这里使用了错误的源表达式。您正在正确读取名称并使用 JSONPath 表达式将其保存到属性中。请注意这里 json-eval() 表示您在这里使用的是 JSONPath。默认是 XPATH(这就是为什么!!!)。

    创建属性后,该属性将驻留在消息上下文中。要读取消息上下文中的属性,您需要使用$ctx:uri.var.name$ctx 表示您正在从消息上下文中读取它。 JSONPath 用于从消息负载中读取,而不是从消息上下文中读取。

    根据以上信息,如下更改您的切换中介。

    <switch source="$ctx:uri.var.name">
       <case regex="Nick|Jade">
          <send>
             <endpoint>
                <http method="get" uri-template="https://backend1.com" />
             </endpoint>
          </send>
       </case>
       <case regex="Tom|Dave">
          <send>
             <endpoint>
                <http method="get" uri-template="https://backend2.com" />
             </endpoint>
          </send>
       </case>
       <default />
    </switch>
    

    请查看以下文档作为参考。 https://docs.wso2.com/display/EI660/Accessing+Properties+with+XPath#AccessingPropertieswithXPath-SynapseXPathVariables https://docs.wso2.com/display/EI660/Working+with+JSON+Message+Payloads+#WorkingwithJSONMessagePayloads-AccessingcontentfromJSONpayloads

    【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多