Code Monkey home page Code Monkey logo

sample-spring-boot-data-mongodb-embedded's Introduction

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

sample-spring-boot-data-mongodb-embedded's People

Contributors

alexbt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sample-spring-boot-data-mongodb-embedded's Issues

Embedded Mongo DB not working

Hi Alex,

I am trying to create a spring boot application with embedded mongo DB as per your shared code, i am getting the below error:

"org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.