繁体   English   中英

WSO2 EI/ESB:使用正则表达式实现 Switch mediator 和 Filter mediator

[英]WSO2 EI/ESB: Implementing Switch mediator and Filter mediator using Regular Expression

在我的 API 中,我有如下属性:

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

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

例如,如果属性可以有 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 mediator 中定义 Source 和 Regex的正确方法是什么?

同样在Filter mediator中也一样

您在此处对 Source 使用了错误的表达式。 您正在正确读取名称并使用 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 -从 JSON 有效负载访问内容

暂无
暂无

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

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