A Spring Boot sample app that integrates MongoDB using Spring Data MongoDB.
It could be used as boilerplate for developing microservices that require a persistent document store.
MongoDB is a NoSQL document database. It stores data in JSON-like documents. Spring Data provides a model and repository layer for integrating with MongoDB.
The fastest way to get up and running is to use the Docker image:
docker pull mongo;
docker run -d -p 27017-27019:27017-27019 --name my-mongodb mongo:latest
Once it's up and running, you can attach to it to run MongoDB commands:
docker exec -it my-mongodb bash
Launch the Mongo Shell client:
mongo
Check it's working ok, e.g. list the dbs:
show dbs
The alternative is to install it natively as per the official guide.
You'll need JDK 11 installed on your dev box.
You can use Gradle or Maven to build the app.
Both Gradle Wrapper and Maven Wrapper are included in the project.
- From the project root, run
./gradlew build
- To generate the Javadoc, run
./gradlew javadoc
and look in the./build/docs/javadoc
folder.
- From the project root, run
./mvnw clean install
- Take a look at the Javadoc in the
./target/apidocs
folders after the build completes.
The app uses the Google Style Guide which is enforced during both the Gradle and Maven build - see the build.gradle and pom.xml files respectively. The Checkstyle report locations are:
- Gradle -
./build/reports/checkstyle/main.html
- Maven -
./target/checkstyle-result.xml
Code coverage is provided by JaCoCo and is enforced at build time. It's currently set to 80% line coverage. See the build files. The coverage report locations are:
- Gradle -
./build/report/jacoco/test/html/index.html
- Maven -
./target/jacoco-report/index.html
SpotBugs is run at build time. Any bugs found will fail the build. The bug report locations are:
- Gradle -
./build/report/jacoco/test/html/index.html
- Maven -
./target/spotbugsXml.xml
Unit tests are run as part of both Gradle and Maven builds.
JUnit, Spring Boot Test , and Flapdoodle's embedded MongoDB is used to unit test the code.
You'll need to stop any other instance of MongoDB that you have running, otherwise the tests will fail to due to port clashes.
The unit test report locations are:
- Gradle -
build/reports/tests/test/index.html
- Maven -
./target/surefire-reports
The integration tests require a running instance of MongoDB - Testcontainers is used to achieve this. Don't forget to stop any other instances of MongoDB you have running before running the tests.
The IT tests are located here.
To run the IT tests:
- Gradle -
./gradlew integrationTests
- Maven
./mvnw clean install -Pint
The IT report locations are:
- Gradle -
./build/reports/tests/integrationTests/index.html
- Maven -
./target/failsafe-reports
The configuration is held in the ./config/application.properties file.
You'll need to change the MongoDB connection details of you are running the database on a different machine:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=sample-app
MongoDB authentication is not enabled for this demo - see the MongoDB Manual and the Spring Data Docs if you need to enable it.
The Application
simply creates some
Users and dummy SIP Registrations, and
then fetches them. Check out the app.log file.
From the the app from the project root folder using:
- Gradle -
./gradlew bootRun
- Maven -
./mvnw spring-boot:run
Once you've run the app, you can take a look at the collections in the database using the Mongo Shell.
To see all the Users and Registrations:
show dbs
use sample-app
db.user.find()
db.registration.find()
The Mongo Shell Manual lists other useful commands.
Spring Data MongoDB uses the MongoTemplate to execute the queries behind the generated find*
methods.
You can use the template directly for more complex queries - see the
CustomRegistrationRepositoryImpl
.
You can use the
@Indexed
annotation on domain objects like
Registration
class.
To see the indexes on the collections:
db.user.getIndexes()
db.registration.getIndexes()
Logging for the app is provided by log4j.
The log file is written to logs/app.log
using a rolling policy. When a log file size reaches
100 MB or a new day is started, it is archived and a new log file is created.
The app will create up to 7 archives on the same day; these are stored in a directory based on the current year and month. Only the last 90 archives are kept. Each archive is compressed using gzip.
The logging level is set at info
. You can change this default logging configuration in
the config/log4j2.xml
file.
Issues and new features are managed using the project Issue Tracker - submit bugs here.
You are welcome to take on new features or fix bugs! See here for how to get involved.