Giter Club home page Giter Club logo

java-spring-boot-mongodb-starter's Introduction

Quick Start: Java, Spring Boot & MongoDB Template

Blog Post

The code in this repository is discussed in this blog post in the MongoDB Developer Center.

Supported versions:

  • Java 21
  • Spring boot 3.2.2
  • MongoDB 7.0
  • MongoDB Java driver 4.11.1
  • Maven 3.8.7
  • OpenAPI 3

MongoDB Atlas

Commands

  • Start the server in a console with mvn spring-boot:run.
  • If you add some Unit Tests, you would start them with mvn clean test.
  • You can start the end-to-end tests with mvn clean integration-test.
  • You can build the project with : mvn clean package.
  • You can run the project with the fat jar and the embedded Tomcat: java -jar target/java-spring-boot-mongodb-starter-1.0.0.jar but I would use a real tomcat in production.

Project Loom & Virtual Threads

  • This project starter supports Virtual Threads thanks to:
    • JDK 21
    • Spring 3.2.0+.
    • spring.threads.virtual.enabled=true in the application.properties

Swagger & OpenAPI 3

Features showcase

This project showcases several features of MongoDB:

  • MongoDB multi-document ACID transactions for 3 functions. See MongoDBPersonRepository.saveAll().
  • MongoDB Aggregation pipeline. See MongoDBPersonRepository.getAverageAge().
  • Implementation of basic CRUD queries. See MongoDBPersonRepository.java.
  • MongoDB typed collection with automatic mapping to POJOs using codecs: See ConfigurationSpring.java.
  • How to manipulate correctly ObjectId across, the REST API, the POJOs and the database itself. See the main trick in Person.java.

And some other cool stuff:

  • You can change the default Spring Boot logo by adding a banner.txt file in your properties.
  • You don't have to use Spring Data MongoDB. The MongoDB driver is more flexible and already provides everything you need to code efficiently and optimise your queries correctly.

Example API Calls

curl -X 'POST' \
  'http://localhost:8080/api/person' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "firstName": "Maxime",
  "lastName": "Beugnet",
  "age": 35,
  "address": {
    "number": 123,
    "street": "avenue des Champs-Elysées",
    "postcode": "75000",
    "city": "Paris",
    "country": "France"
  },
  "insurance": true,
  "cars": [
    {
      "brand": "Peugeot",
      "model": "3008",
      "maxSpeedKmH": 280
    }
  ]
}'
curl -X 'GET' 'http://localhost:8080/api/persons' -H 'accept: */*'

Author

Maxime Beugnet

java-spring-boot-mongodb-starter's People

Contributors

mabeulux88 avatar

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.