This is an example of Spring Boot with embedded MongoDB.
-
MongoConfiguration
to configure embedded MongoDB. -
CustomMongoRepository
andCustomSimpleMongoRepository
creates a custom Mongo repository. It's responsible for generating and setting entity/document id field during insertion if the id is null and it's of type UUID. -
BookRepository
responsible for CRUD operations onBook
entity/document.
The spring-boot-starter-data-mongodb.jar
provides all Spring related MongoDB
classes including MongoRepository
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
The de.flapdoodle.embed.mongo.jar
downloads and starts a MongoDB instance.
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
The embedmongo-spring.jar
provides Spring Factory Bean for
Embedded MongoDB.
<dependency>
<groupId>cz.jirutka.spring</groupId>
<artifactId>embedmongo-spring</artifactId>
<version>1.3.1</version>
</dependency>
To build the JAR, execute the following command from the parent directory:
mvn clean install
Run the executable jar from the command to start the application,
java -jar springboot-mongodb-example-1.0.0.jar
Once the application starts up at port 8080
, you can access the swagger UI at
http://localhost:8080/swagger-ui.html
. From the UI, you can create and retrieve
book entities.
Basic authorization is enambled, you have to use admin/admin
credentials
for creating and retrieving books.