Morphia integration for Dropwizard
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status Coverage Status Download

Morphia integration for Dropwizard

Add Dependency

Create settings.xml in your project dir

<?xml version="1.0" encoding="UTF-8" ?>
<settings xsi:schemaLocation=''
          xmlns='' xmlns:xsi=''>

Add dependency to pom.xml



Add the following to your Dropwizard Configuration class

private MongoConfiguration mongo;

public MongoConfiguration getMongo() {
    return mongo;

public void setMongo(MongoConfiguration mongo) {
    this.mongo = mongo;

Add the following to your Dropwizard Application class

private MorphiaBundle<ExampleConfiguration> morphiaBundle =
        new MorphiaBundle<ExampleConfiguration>(ExampleEntity.class) {
            protected MongoConfiguration getMongo(ExampleConfiguration configuration) {
                return configuration.getMongo();
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {

public void run(ExampleConfiguration configuration, Environment environment) throws Exception {
    Datastore datastore = morphiaBundle.getDatastore();

It is also possible to create an instance of MorphiaPackageBundle, which accepts package name as constructor parameter.

private static MorphiaPackageBundle<ExampleConfiguration> morphiaBundle = 
        new MorphiaPackageBundle<ExampleConfiguration>("some.package.with.entities", false) {
            protected MongoConfiguration getMongo(DummyConfiguration configuration) {
                return configuration.getMongo();


Minimal configuration requires the following

  type: uriConfig
  uri: ${MONGODB_URI}

Currently the only available type is uriConfig. A mongodb:// uri is used to connect to Mongo. dbName can be retrived from uri, if it is specified. Otherwise you'll have to set dbName inside mongo explicitly.

Available Morhpia settings:

  • storeNulls (boolean) Enables/disables storing null values in Mongo
  • storeEmpties (boolean) Enables/disables storing empty values in Mongo
  • enableValidationExtension (boolean) Add Hibernate Validator interceptor to Morphia
  • ignoreFinals (boolean) Specifies whether Morphia will ignore final fields
  • useLowerCaseCollectionNames (boolean) Specifies whether Morphia should use lowercase letters for collection names


Example app with a bit of Morphia usage is available here