Skip to content

OKaluzny/spring-boot-rest-api-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot REST API with MongoDB

CI Spring Boot Java MongoDB License

A modern RESTful API for managing books, built with Spring Boot 4 and MongoDB.

Features

  • RESTful API with CRUD operations
  • Spring Boot 4.0.2 with Spring Framework 7
  • MongoDB with embedded database for development
  • Spring Security with Basic Authentication
  • Spring Data REST with HAL support
  • Bean Validation (Jakarta Validation)
  • Lombok for boilerplate reduction
  • JUnit 5 tests with embedded MongoDB

Tech Stack

Technology Version
Java 21
Spring Boot 4.0.2
Spring Framework 7.0.3
MongoDB Driver 5.6.2
Embedded MongoDB 6.0.9
Apache Tomcat 11.0.15
JUnit 5

Project Structure

src/
├── main/
│   ├── java/com/kaluzny/
│   │   ├── Application.java           # Main entry point
│   │   ├── domain/
│   │   │   ├── Book.java              # Entity model
│   │   │   └── BookRepository.java    # Data repository
│   │   └── web/
│   │       └── BookController.java    # REST controller
│   └── resources/
│       └── application.yml            # Configuration
└── test/
    └── java/com/kaluzny/
        └── AppTest.java               # Integration tests

Getting Started

Prerequisites

  • Java 21 or higher
  • Maven 3.8+

Run the Application

mvn spring-boot:run

The application will start on http://localhost:8080

Run Tests

mvn test

API Reference

Authentication

All endpoints require Basic Authentication:

  • Username: user
  • Password: user

Endpoints

Method Endpoint Description
GET /api/books Get all books
GET /api/books/{id} Get book by ID
POST /api/books Create a new book
DELETE /api/books/{id} Delete book by ID
DELETE /api/books Delete all books

Examples

Get All Books

curl -u user:user http://localhost:8080/api/books

Response:

[
  {
    "id": 1,
    "name": "Java SE",
    "description": "Programming"
  },
  {
    "id": 2,
    "name": "Java EE",
    "description": "Programming"
  }
]

Get Book by ID

curl -u user:user http://localhost:8080/api/books/1

Response:

{
  "id": 1,
  "name": "Java SE",
  "description": "Programming"
}

Create a Book

curl -u user:user -X POST http://localhost:8080/api/books \
  -H "Content-Type: application/json" \
  -d '{"id": 3, "name": "Spring in Action", "description": "Framework"}'

Delete a Book

curl -u user:user -X DELETE http://localhost:8080/api/books/3

Data Model

Book

Field Type Constraints
id Integer Primary Key
name String Required, min length 1
description String Required, min length 1

Configuration

application.yml

spring:
  data:
    mongodb:
      database: Books
  security:
    user:
      name: user
      password: user

server:
  port: 8080

Environment Variables

Variable Description Default
SERVER_PORT Application port 8080
SPRING_DATA_MONGODB_URI MongoDB connection URI embedded
SPRING_SECURITY_USER_NAME Auth username user
SPRING_SECURITY_USER_PASSWORD Auth password user

Build

Create JAR

mvn clean package -DskipTests

Run JAR

java -jar target/Simple\ RESTful\ Web\ Service\ with\ Spring\ Boot\ and\ MongoDB.jar

Docker (Optional)

FROM eclipse-temurin:21-jre
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
docker build -t spring-boot-mongodb .
docker run -p 8080:8080 spring-boot-mongodb

CI/CD

This project uses GitHub Actions for continuous integration.

Pipeline

Job Description
Build & Test Compiles code, runs tests, packages JAR
Code Quality Generates JaCoCo coverage report

Workflow Triggers

  • Push to master or main branch
  • Pull requests to master or main branch

Artifacts

Each successful build produces:

  • app-jar — Application JAR file
  • coverage-report — JaCoCo test coverage report

License

This project is licensed under the MIT License.

Author

Oleg Kaluzny


Made with Spring Boot 4

About

Simple REST API with Spring Boot and MongoDB.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages