[英]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
如果該屬性的名稱為 Nick 或 Jade,它將指向 back-end-1。
如果該屬性的名稱為 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.