You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ml-gradle在MarkLogic中安装/扩展自定义REST扩展?

使用ml-gradle安装自定义REST端点扩展

ml-gradle完全支持自动化部署MarkLogic REST扩展,无需手动执行curl命令。以下是具体配置步骤和示例:

1. 目录结构约定

ml-gradle会自动扫描src/main/ml-config/rest/extensions目录下的扩展文件,按以下结构组织:

src/
└── main/
    └── ml-config/
        └── rest/
            └── extensions/
                ├── my-custom-endpoint.xqy  # 扩展实现代码(XQuery)
                └── my-custom-endpoint.xml  # 扩展元数据配置(可选)

2. 配置build.gradle

确保引入ml-gradle插件,并指定你的REST API实例信息(关联到已创建的HTTP应用服务器):

plugins {
  id "com.marklogic.ml-gradle" version "4.3.0" // 使用最新稳定版本
}

// 配置REST API实例,与你的HTTP应用服务器对应
mlRestApis {
  myCustomRestApi {
    name = "My Custom REST API"
    port = 8000 // 你的HTTP应用服务器端口
    database = "my-content-database"
    modulesDatabase = "my-modules-database"
  }
}

3. 编写自定义REST扩展代码

XQuery示例(my-custom-endpoint.xqy)

xquery version "1.0-ml";
module namespace ext = "http://example.com/rest/extensions/my-custom-endpoint";

declare
  %rest:path("/my-custom-endpoint")
  %rest:GET
  %rest:produces("application/json")
function ext:get-endpoint() {
  xdmp:to-json(
    map:new((
      map:entry("message", "Hello from custom REST endpoint"),
      map:entry("timestamp", xdmp:current-dateTime())
    ))
  )
};

JavaScript示例(my-custom-endpoint.sjs)

function handleGetRequest(context, params) {
  return {
    message: "Hello from JavaScript custom endpoint",
    timestamp: fn.currentDateTime()
  };
}

exports.GET = handleGetRequest;

4. 扩展元数据配置(可选)

如果需要设置扩展的权限、描述等元数据,创建同名的XML文件(如my-custom-endpoint.xml):

<rapi:extension xmlns:rapi="http://marklogic.com/rest-api">
  <rapi:name>my-custom-endpoint</rapi:name>
  <rapi:description>Sample custom REST endpoint for business logic</rapi:description>
  <rapi:required-permission>
    <rapi:role-name>rest-reader</rapi:role-name>
    <rapi:capability>read</rapi:capability>
  </rapi:required-permission>
</rapi:extension>

5. 部署扩展

执行以下Gradle命令完成部署:

./gradlew mlDeploy

ml-gradle会自动将扩展文件上传到指定的REST API实例,替代手动curl的安装流程。部署完成后,即可通过http://<host>:8000/v1/resources/my-custom-endpoint访问自定义端点。

内容的提问来源于stack exchange,提问作者Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 20:56:08