Skip to content

alexbt/sample-spring-boot-data-mongodb-embedded

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot, Spring Data MongoDB and Embedded MongoDB

In almost all of my projects that involves external resources, I try my best to enable the application to fully run without dependencies. It's useful to provide a fully working backing This sample project shows how a spring-boot application can be setup with an embedded MongoDB. The focus of this project is to show how to configure an embedded database with Spring Boot, however the source code also contains a RestController and a Spring Data Repository.

Other sample projects with embedded databases

Step by step

Maven dependencies To load an embedded MongoDB with Spring Boot, all you need is to add its maven dependency into your pom. The rest will be taken care of. MongoDB binaries will even be downloaded on the fly at build time.

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <version>1.50.5</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

Spring Boot configuration Absolutely no configuration is required. By default, the embedded database will be started with url mongodb://localhost:27017/test Spring Boot's EmbeddedMongoAutoConfiguration, the embedded database will be detected and a datasource pointing to it will be created.

If you have a mongo client installed you may access it normally:

$ mongo localhost:27017/test
$ show collections

For now, your mongo instance is probably empty since you have not yet added anything. Setup a Spring Data MongoRepository, start adding collections or do it from the command line:

$ db.yourcollection.insertOne({"field":"value"})

That's it

Assuming you have a Spring Boot entry point, launch it:

@SpringBootApplication
public class Launcher {
    
    public static void main(String[] args){
        new SpringApplicationBuilder() //
        .sources(Launcher.class)//
        .run(args);
    }
}

Get the code - do it

Clone the repository:

$ git clone https://github.com/alexturcot/sample-spring-boot-data-mongodb-embedded.git

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages