How to connect the Spring data to Marklogic

Bearded confident maintenance engineer in white shirt is working in database center
Reading Time: 2 minutes

Implementation Spring Data MarkLogic :

An implementation of the Spring Data MarkLogic NoSQL database that supports the following features:

  • Spring configuration support using Java-based @Configuration classes
  • MarkLogicTemplate helper class for common MarkLogic operations. Integrated object mapping between documents and POJOs (using Jackson by Default).
  • Exception translation into Spring’s portable Data Access Exception hierarchy
  • Automatic implementation of Repository interfaces including support for custom finder methods.

How to connect the Spring Data to Marklogic :

Spring MarkLogic support requires MarkLogic 8 or above and Java 8 or above. We will need a running MarkLogic server in order to get started. 

First of all, we will need Marklogic to be installed.

Spring Data Marklogic connects to Marklogic using the DatabaseClient, so you need a DatabaseClient server up and ready.

Then add the Spring Data Marklogic dependency to your project.

Include the following dependency in your project’s pom.xml dependencies section:






Create a simple Person class that you can use to save person information:

package com.knoldus.springdata.SpringDatawithMarklogic;


public class Person {

    private int id;

    private String name;

    private int age;

    public Person() {}

    public Person(int id, String name, int age) { = id; = name;

        this.age = age;

    public int getId() {

        return id;


    public String getName() {

        return name;


    public int getAge() {

        return age;



    public String toString() {

        return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";


And an application:

package com.knoldus.springdata.SpringDatawithMarklogic;

import com.marklogic.client.DatabaseClientFactory.DigestAuthContext;

import com.marklogic.client.query.StructuredQueryBuilder;

import com.knoldus.springdata.SpringDatawithMarklogic.MarkLogicOperations;

import com.knoldus.springdata.SpringDatawithMarklogic.MarkLogicTemplate;

import static com.marklogic.client.DatabaseClientFactory.newClient;

public class MarkLogicApp {

    private static final StructuredQueryBuilder qb = new StructuredQueryBuilder();

    public static void main(String[] args) throws Exception {

        MarkLogicOperations ops = new MarkLogicTemplate(newClient("localhost", 8000, new DigestAuthContext("admin", "admin")));
        ops.write(new Person(1,"Bobby", 23));

        Person bobby = ops.searchOne(qb.value(qb.jsonProperty("name"), "Bobby"), Person.class);




This will produce output similar to the following:

Person [id=1, name=Bobby, age=23]

There are a few things of note in this example:

  • By default, this uses Jackson to serialize your POJOs for persistence in the database, and deserialize them when pulling data out of the database. There is an empty default constructor because Jackson creates the POJO instance first before setting the fields from the data
  • An @Id annotation is required to identify the field that will hold the document identifier. Internally documents are stored under a URI, so a person with an id of “1” would be saved under the URI “/Person/1.json”
  • You can create the template class for MarkLogic, MarkLogicTemplate, by passing in a DatabaseClient that you get from the DatabaseClientFactory

Connecting to MarkLogic with Spring

The simplest way to connect to MarkLogic is to create a @Bean that creates a template object:

package com.knoldus.springdata.SpringDatawithMarklogic;

import com.marklogic.client.DatabaseClient;

import com.marklogic.client.DatabaseClientFactory.DigestAuthContext;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.knoldus.springdata.SpringDatawithMarklogic;.MarkLogicOperations;

import com.knoldus.springdata.SpringDatawithMarklogic;.MarkLogicTemplate;

import static com.marklogic.client.DatabaseClientFactory.newClient;


public class RepositoryConfiguration {


    public MarkLogicOperations marklogicTemplate() {

        DatabaseClient client = newClient("localhost", 8000, new 
DigestAuthContext("admin", "root"));

        return new MarkLogicTemplate(client);


This will automatically be wired into any of your @Repository annotated classes (or ones that extend MarkLogicRepository). You can also explicitly auto-wired it in your other Spring classes:


private MarkLogicOperations ops;

References :–sample-app

Written by 

Strong in design and integration problem-solving skills. Experience in Java/J2EE with database analysis and design. Skilled in developing business plans, requirements specifications, user documentation, and architectural systems research. Having Good Work Experience with Core Java, Advanced Java, Typescript, and Related Technologies, AWS like S3, Lambda, EC2, Elemental Live, Media Live, Tesseracts, and Textract.