1

在我的 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中也一样

4

1 回答 1

1

您在此处对 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 有效负载访问内容

于 2020-05-13T18:04:36.897 回答