An initializer is a class with the method init() that is invoked at RESTHeart startup.

It can be used to perform initialization code. For instance, it can programmatically add Transformers and Checkers and init the db.

The Initializer class can be specified in the configuration file

### Initializer

# A custom initializer implmenting the org.restheart.init.Initializer interface
# Can be used to inizialize data or add global transformers, checkers or security predicates
# See org.restheart.init.TestInitializer for a simple example

initializer-class: org.restheart.init.TestInitializer

For an example look at org.restheart.init.TestInitializer

The class must implement the Initializer interface

package org.restheart.init;

public interface Initializer {
    public void init();

Best practices

Get the MongoClient

// get the MongoClient
MongoClient client = MongoDBClientSingleton.getInstance().getClient();

Add Global Transformers

Global Transformers are applied to all requests.

See Request Transformers for more information on GlobalTransformer

GlobalTranformer globalTranformer;

// transform the request

// transform the response

Add Global Checkers

Global Checkers are applied to all requests.

See Request Checkers for more information on GlobalChecker.

// check the request
GlobalChecker globalChecker;


Add Global Security Predicates

Global Security Predicates are applied to all requests.

// allow users with role "ADMIN" to GET /
RequestContextPredicate securityPredicate = new RequestContextPredicate() {
            public boolean resolve(HttpServerExchange hse, RequestContext context) {
                return context.isRoot()
                        && context.isGet()
                        && context.getAuthenticatedAccount() != null
                        && context.getAuthenticatedAccount().getRoles().contains("ADMIN");

// add the global predicate