【问题标题】:WSO2 API Manager v1.8.0WSO2 API 管理器 v1.8.0
【发布时间】:2015-04-03 07:57:56
【问题描述】:

我有一个将路径参数从 API 使用者映射到后端 API 端点的简单用例。我已经做了很多研究,但还没有找到关于如何做到这一点的具体答案。据我了解,如果不使用uri-template,就无法完成路径参数的映射。现在的问题是 API Manager 不支持来自 API Publisher 用户界面的 uri-template,您必须改用 url-mapping。 WSO2 开发人员的一篇博客说,您可以转到单个突触配置并手动将其更改为 uri-template。但是在实践中发生的是,对 synapde 配置的更新以某种方式触发了数据库更新,否则会从发布者 UI 发生,最终结果是它不起作用。有人可以提供有关如何映射路径参数的方法吗?仅供参考 - 查询参数映射对我有用,因为它不需要 uri-template 并且可以使用 url-mapping 本身来实现。

【问题讨论】:

    标签: wso2 wso2-am uritemplate api-manager path-parameter


    【解决方案1】:

    APIM 1.8 支持 uri-template 和 url-mapping。您可以在资源部分定义您的 url-template(或者如果您想要 uri-mapping)。请查看我添加了 url-template 和 url-mapping 的突触配置

    在发布者中,我添加了 /json 作为 url 映射的 url 模式和 /json/{id} 用于 uri-template

        <?xml version="1.0" encoding="UTF-8"?>
    <api xmlns="http://ws.apache.org/ns/synapse"
         name="admin--mytst"
         context="/mytest"
         version="1"
         version-type="url">
       <resource methods="GET" url-mapping="/json" faultSequence="fault">
          <inSequence>
             <filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
                <then>
                   <send>
                      <endpoint name="admin--mytst_APIproductionEndpoint_0">
                         <http uri-template="http://localhost.com"/>
                      </endpoint>
                   </send>
                </then>
                <else>
                   <sequence key="_sandbox_key_error_"/>
                </else>
             </filter>
          </inSequence>
          <outSequence>
             <send/>
          </outSequence>
       </resource>
       <resource methods="GET" uri-template="/json/{id}" faultSequence="fault">
          <inSequence>
             <filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
                <then>
                   <send>
                      <endpoint name="admin--mytst_APIproductionEndpoint_1">
                         <http uri-template="http://localhost.com"/>
                      </endpoint>
                   </send>
                </then>
                <else>
                   <sequence key="_sandbox_key_error_"/>
                </else>
             </filter>
          </inSequence>
          <outSequence>
             <send/>
          </outSequence>
       </resource>
       <handlers>
          <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>
          <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler">
             <property name="id" value="A"/>
             <property name="policyKey" value="gov:/apimgt/applicationdata/tiers.xml"/>
          </handler>
          <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageHandler"/>
          <handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtGoogleAnalyticsTrackingHandler">
             <property name="configKey" value="gov:/apimgt/statistics/ga-config.xml"/>
          </handler>
          <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
       </handlers>
    </api>`enter code here`
    

    【讨论】:

    • 谢谢。但是,需要解决一个后续问题。您能否解释一下我如何从公开的 URL 中读取 {id} 路径参数并将其作为目标 URL 的查询参数传递给“localhost.com">”?
    • 使用 {uri.var.id} 并附加到 traget URL[1] 1.docs.wso2.com/display/ESB470/HTTP+Endpoint
    • 是的——我终于可以让它工作了。感谢您的回答。这里的关键是 API Manager 发布者界面和对突触配置文件的手动编辑需要始终保持同步。这是导致问题的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多