The module sda-commons-server-spring-data-mongo is used to work with MongoDB using Spring Data Mongo.
The SpringDataMongoBundle
should be added as a field in the application class instead of being anonymously added in the initialize
method like other bundles of this library.
The Dropwizard application's config class needs to provide a
MongoConfiguration
.
Please refer to the official documentation how to annotate your entity classes correctly, e.g. by
adding @Document
, @MongoId
or @Indexed
.
public class MyApp extends Application<MyConfiguration> {
private final SpringDataMongoBundle<MyConfiguration> springDataMongoBundle =
SpringDataMongoBundle.builder()
.withConfigurationProvider(MyConfiguration::getSpringDataMongo)
.withEntites(MyEntity.class)
.build();
private PersonRepository personRepository;
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(springDataMongoBundle);
}
@Override
public void run(MyConfiguration configuration, Environment environment) {
personRepository = springDataMongoBundle.createRepository(PersonRepository.class);
}
public MongoOperations getMongoOperations() {
return springDataMongoBundle.getMongoOperations();
}
public GridFsOperations getGridFsOperations() {
return springDataMongoBundle.getGridFsOperations();
}
public PersonRepository getPersonRepository() {
return personRepository;
}
}
The database connection is configured in the config.yaml
of the application, specifically the
connectionString
.
mongo:
connectionString: "${MONGODB_CONNECTION_STRING:-}"
Example config for developer machines using local-infra:
mongo:
connectionString: "mongodb://mongo-1:27118,mongo-2:27119,mongo-3:27120/myAppName?replicaSet=sda-replica-set-1"
In tests the config is derived from the MongoDbClassExtension
. See
sda-commons-server-mongo-testing
for details.
It is strongly recommended to annotate all types that are used in a field that does not exactly
match the type with @TypeAlias
.
Using @TypeAlias
will replace the default class name as discriminator with the given value in the
annotation and gives you the ability for refactoring of the model classes.
This rule applies for all types that are a subclass of an (abstract) super class, types that are
stored in a field defined as Object
and all types that are stored in a shared collection.
The latter are usually a subtype of an abstract class to support a common repository.
It is important to register each class that is annotated with @TypeAlias
by using withEntities
in the builder of the bundle.
If not registered there, the mapping is unknown when reading entities.
A health check with the name mongo is automatically registered to test the mongo connection. A simple ping command to the database is used.
The bundle will create indexes automatically by default. You can change the configuration using the builder:
private final SpringDataMongoBundle<MyConfiguration> springDataMongoBundle =
SpringDataMongoBundle.builder()
.withConfigurationProvider(MyConfiguration::getSpringDataMongo)
.withEntites(MyEntity.class)
.disableAutoIndexCreation()
.build();
The bundle support creating Spring Data Mongo repositories that are defined by an interface. You
can create an instance of your repository using the bundle's createRepository
method that
accepts the interface.
public interface PersonRepository extends PagingAndSortingRepository<Person, ObjectId> {
// additional custom finder methods go here
}
var personRepository = springDataMongoBundle.createRepository(PersonRepository.class);
Instead of providing caCertificate
as an environment variable, mount the CA certificates in PEM format
in the directory /var/trust/certificates
. Certificates available in subdirectories will also be loaded.
Note that this directory is also configurable through the Dropwizard config class. The config class should then provide a
CaCertificateConfiguration
to the bundle builder. See sda-commons-shared-certificates
for details.