1. Add XQRS to an existing Gradle / Data Hub Project

Add the XQRS plugin to the plugins section of your existing build.gradle file, e.g.

plugins {
  id "com.marklogic.ml-data-hub" version "5.0.0"
  id "com.xmllondon.xqrs-gradle-plugin" version "1.0.2"
}

Initialize XQRS by running the following command

$ gradle xqrsInit

This will prompt you for the port you want the XQRS HTTP Server to listen on, the default port chosen for you is 8022 but you can change this. The task will create the following files in your ml-gradle project:

src/
├── main/
│   ├── ml-modules/ 
│   │   └── root/
│   │       ├── example.xqy
│   │       ├── example-guestbook.xqy
│   │       ├── guestbook.css 
│   │       └── xqrs/ <-- XQRS core library installation
│   │           └── xqrs.xqy
│   └── ml-config/
│       └── servers/
│           └── xqrs-server.json <-- XQRS HTTP Server Config

2. Creating RESTXQ Modules

Write RESTXQ XQuery Modules and place them anywhere underneath the ml-modules/root directory or any of it's sub-directories. An example module would look like

xquery version "1.0-ml";

module namespace example = "http://www.example.org/example";
declare namespace rest = "http://exquery.org/ns/restxq";

declare
  %rest:path("/hello-world")
function hello-world() {
  "Hello World"
};

declare
  %rest:path("/echo/{$param}")
function echo($param as xs:string) {
  $param
};

3. Deploy to MarkLogic and check that it's all working

In the project directory, run the command

$ gradle mlDeploy

The mlDeploy task will ensure the XQRS HTTP Server is setup OK as well as uploading all your XQuery files to your Modules database.

Now point your browser at the host where you've installed MarkLogic and the XQRS HTTP Server

http://localhost:8022/hello-world

You should see a response like

Hello World

Also, pointing your browser to "/echo/whasup" should return the plain text response of

whasup

And you could sign your own MarkLogic Guestbook at /guestbook

Any request that does not match a function's criteria will be passed directly through to the MarkLogic App Server itself as if no url rewriter was in place, so you can serve images and other media files directly from their normal paths as you would with a normal Web App or even include other existing .xqy files that can live side by side with your new RESTXQ Modules.

TOP