Skip to content

mongodb-developer/spring-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot + MongoDB REST API Example

This is a basic CRUD API built with Spring Boot and MongoDB. It shows how to:

  • Create, read, update, and delete documents in MongoDB
  • Use Spring Data MongoDB with MongoRepository
  • Add validation with DTOs using Jakarta Bean Validation
  • Return clean, shaped API responses

Prerequisites

  • Java 21
  • Maven 3.9+
  • MongoDB running locally or a MongoDB Atlas URI

How to Run

  1. Clone the project
  2. Set your MongoDB URI in src/main/resources/application.properties:
spring.data.mongodb.uri=YOUR\_CONNECTION\_STRING
spring.data.mongodb.database=library
  1. Start the application:
mvn spring-boot:run
  1. Test the API with curl, Postman, or HTTPie.

Example POST:

curl -X POST http://localhost:8080/api/books \
  -H "Content-Type: application/json" \
  -d '{"title": "Dune", "author": "Frank Herbert"}'

Project Structure

  • Book: The domain model
  • BookRequest: Input DTO with validation
  • BookResponse: Output DTO for shaping API responses
  • BookRepository: Spring Data MongoDB repository
  • BookController: REST controller

About

A RESTful API with simple validation, using Spring and MongoDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages