无法根据 WSO2 API 管理器中的消息中介策略调用动态端点/URL [英] Not able to call Dynamic Endpoints/URLs on the basis of Message Mediation polices in WSO2 API Manager

查看:23
本文介绍了无法根据 WSO2 API 管理器中的消息中介策略调用动态端点/URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 APIM-3.1.0,我需要根据标头或请求参数重定向 API.我尝试过请求参数,但无法调用不同的 API.我使用了以下自定义中介策略并将其添加到测试 API,但无法调用不同的 URL.每次我调用 API 时,即使我将操作的值作为菜单传递,我也会在下面的代码中获得 else 部分的输出(else 部分中提到的 URL).

I'm using APIM-3.1.0 and I need to Redirect APIs’ based upon header or request parameter. I have tried for request parameter but unable to call different API's. I have used below custom mediation policy and added it to a test API, but unable to call the different URLs. Every time I was calling API, I was getting output for the else part (URL mention in the else part) in below code even if I am passing the value of operation as menu.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic-endpoint-seq">
<property expression="json-eval($.operation)" name="operation" />
<filter regex="menu" source="$ctx:operation">
<then>
    <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
    <header name="To" expression="get-property('C')"/> 
</then>
<else>
    <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
    <header name="To" expression="get-property('B')"/>
</else>
</filter>
</sequence>

即使传递如下所示的参数值,我也在控制台中收到警告:

I was getting warning in Console even passing value of parameter as shown below:

[2020-07-24 17:20:38,643] 警告 - SynapseJsonPath Json Payload 为空.

[2020-07-24 17:20:38,643] WARN - SynapseJsonPath Json Payload is empty.

有没有办法做同样的事情,或者中介政策有什么错误?

Is there a way to do the same or there is any error in mediation policy?

推荐答案

在这里,您可以使用一些变量名在标头中传递值,并在中介策略中定义相同的值.

Here, you can pass value in header with some variable name and define the same in mediation policies.

下面的代码根据变量check"调用不同的端点;存在与否,如果header中存在任何值,endpoint_B将被调用,如果check的值不存在或check不存在于header中,则调用endpoint_C.

The below Code calls different endpoints on the basis of variable "check" is present or not, if it is present in header with any value,the endpoint_B will get called, if value of check is not present or check is not present in header, then it will call endpoint_C.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic-endpoint-seq-boolean">
<!--it checks the value for variabe check in header if there exist value for check 
then it will call B, if value not exist or check is not present then C called-->
<property name="uri.var.check" expression="get-property('transport','check')"/>
<filter source="boolean(get-property('uri.var.check'))" regex="false">
<then>
    <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
    <header name="To" expression="get-property('C')"/> 
</then>
<else>
    <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
    <header name="To" expression="get-property('B')"/>
</else>
</filter>
</sequence>

还有一种方法可以做同样的事情,你可以在 XML 中使用 Switch case,如下所示并配置多个端点.在这里,如果您在标头中将 check 的值作为 'B' 传递,将调用 endpoint_B,否则如果您在标头中将 'check' 的值作为 'C' 传递,那么 endpoint_C 将被调用,如果您传递任何其他值而不是'B' 或 'C' 或不传递任何值,甚至不通过标头中的检查而不是默认端点,这里将调用 endpoint_A.

There is one more way to do the same thing, you can use Switch case in XML,as shown below and configure multiple endpoints. Here, if you pass the value of check as 'B' in header, endpoint_B will called, else if you pass value of 'check' as 'C' in header, then endpoint_C will get called, and if you pass any other value than 'B' or 'C' or not pass any value or even not pass check in header than default endpoint here endpoint_A will get called.

<sequence name="dynamic_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property name="uri.var.check" expression="get-property('transport','check')"/>
<switch source="get-property('uri.var.check')">
    <case regex="B">
       <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
        <property name="B" expression="fn:concat('http://localhost:8080/Test/','getB')"/>
        <header name="To" expression="get-property('B')"/>
    </case>
    <case regex="C">
        <property name="C" expression="fn:concat('http://localhost:8080/Test/','getC')"/>
        <header name="To" expression="get-property('C')"/>
    </case>
    <default>
        <property name="A" expression="fn:concat('http://localhost:8080/Test/','getA')"/>
        <header name="To" expression="get-property('A')"/>
    </default>
</switch>

如果您的端点在相同的主机和端口上运行,则使用 property=rest_url_postfix"还有另一种使用 switch 的方法;如下面的代码所示.在这里,输出将与上面相同,但您需要对上面的代码进行一些更改,您需要在 WSO2-APIM 发布者的 Endpoints 选项卡中选择动态端点,而对于以下代码,您选择 REST Endpoints 并选择端点的值如 http://://.例如,http://localhost:8080/Test/

There is one more way to use switch if your endpoint are running on same host and port by using property="rest_url_postfix" as shown in below code. Here,output will be same as above but some changes that you need to make are for the above code you need to select dynamic endpoint in Endpoints tab in WSO2-APIM publisher while for the below code, you select REST Endpoints and select value of endpoint as http://<host_name>:<port_number>// . For example, http://localhost:8080/Test/

<sequence name="dynamic_same_ep_switch" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<!-- The property which is retrieved as get-property('To')" stores the request URI for the API. Based on this value we will determine the endpoint which the request needs to be routed to.-->
<property name="uri.var.check" expression="get-property('transport','check')"/>
<switch source="get-property('uri.var.check')">
    <case regex="B">
       <!-- We are then assigning the endpoint which we need to route to in a property named service_ep in this step -->
        <property name="REST_URL_POSTFIX" value="getB" scope="axis2"/>
    </case>
    <case regex="C">
        <property name="REST_URL_POSTFIX" value="getC" scope="axis2"/>
    </case>
    <default>
        <property name="REST_URL_POSTFIX" value="getA" scope="axis2"/>
    </default>
</switch>
</sequence>

有关样本中介序列的更多信息,请参阅以下链接:

For anymore information regarding sample mediation sequences refer following links:

希望.这将解决您的查询.

Hope. This resolves your Query.

这篇关于无法根据 WSO2 API 管理器中的消息中介策略调用动态端点/URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆