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
- Java 21
- Maven 3.9+
- MongoDB running locally or a MongoDB Atlas URI
- Clone the project
- Set your MongoDB URI in
src/main/resources/application.properties
:
spring.data.mongodb.uri=YOUR\_CONNECTION\_STRING
spring.data.mongodb.database=library
- Start the application:
mvn spring-boot:run
- 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"}'
Book
: The domain modelBookRequest
: Input DTO with validationBookResponse
: Output DTO for shaping API responsesBookRepository
: Spring Data MongoDB repositoryBookController
: REST controller