我正在扩展MarkLogic中的REST API,并使用gradle管理我的开发和部署。默认情况下,我的端点看起来像这样(注意v1)):
http://localhost:8005/v1/resources/my_endpoint
如果我更改了端点实现,如何将新版本公开为v2并保留原来的(v1))进行向后兼容,这样我就有
http://localhost:8005/v2/resources/my_endpoint
使用它?我在gradle配置文件中没有看到任何注释版本的方法。我一直在搜索MarkLogic的gradle wiki,没有找到答案。
遗憾的是,REST API不允许您想要的-参见https://docs.marklogic.com/REST/PUT/v1/config/resources/[name]。您可以在扩展中指定一个版本作为可选的元数据,但这不会改变URL -它仍然是"/v1/resources/my_endpoint"
但是,如果您自定义MarkLogic用于REST API应用服务器的默认XML重写器,则可以实现这一点-参见https://docs.marklogic.com/guide/app-dev/XMLrewriter。
我能够像@rjrudin建议的那样使用声明式重写器来满足需求。
我首先将<MarkLogic install dir>ModulesMarkLogicrest-apirewriter.xml
复制到api-rewriter.xml
,修改新文件,并更新我的rest api服务器Url Rewriter
值(通过管理控制台)。
然后我添加了以下声明:
<match-path matches="^/api">
<match-method any-of="GET">
<match-path matches="/(vd+.d+)/([w|-]+)/(d+)">
<set-query-param name="name">$1_$2</set-query-param>
<set-query-param name="rs:id">$3</set-query-param>
<dispatch>/MarkLogic/rest-api/endpoints/resource-service-query.xqy</dispatch>
</match-path>
<match-path matches="/(vd+.d+)/(w+)">
<set-query-param name="name">$1_$2</set-query-param>
<dispatch>/MarkLogic/rest-api/endpoints/resource-service-query.xqy</dispatch>
</match-path>
</match-method>
</match-path>
我使用ml-gradle
创建资源实现,其名称如下:v2.0_ping.sjs
。现在我可以使用这样的url:
http://{{host}}:{{port}}/api/v2.0/ping
解析为 的
/MarkLogic/rest-api/endpoints/resource-service-query.xqy?name=v2.0_ping