This repository contains a Spring Boot application that connects to a MongoDB database and includes Docker support for easy deployment.
- RESTful APIs built with Spring Boot.
- MongoDB as the database for persistence.
- Docker support for containerized deployment.
- Easy configuration with environment variables.
Before running the application, ensure you have the following installed:
- Java Development Kit (JDK): Version 17 or later.
- Maven: For building the project.
- Docker: For containerization.
- MongoDB: If running locally.
.
├── src/                  # Application source code
├── target/               # Compiled output (generated after build)
├── Dockerfile            # Docker configuration for the application
├── pom.xml               # Maven dependencies and build configuration
├── README.md             # Project documentation
└── .gitignore            # Ignored files for Git
If you have MongoDB installed locally, ensure it's running on the default port 27017. Update application.properties or application.yml:
spring.data.mongodb.uri=mongodb://localhost:27017/your-database-nameRun MongoDB using Docker:
docker run -d -p 27017:27017 --name mongodb mongoYou can set environment variables to configure the application:
- SPRING_PROFILES_ACTIVE: Set the Spring profile (e.g.,- dev,- prod).
- MONGO_URI: MongoDB connection string.
git clone https://github.com/thusithz/spring-boot-with-mongodb.git
cd spring-boot-with-mongodbUse Maven to build the application:
mvn clean packageRun the application with Maven:
mvn spring-boot:rundocker build -t spring-boot-app .Run the Spring Boot application:
docker run -p 8080:8080 --name spring-boot-app spring-boot-appUse Docker Compose (optional):
Create a docker-compose.yml file:
version: '3.8'
services:
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - "27017:27017"
  spring-boot-app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_DATA_MONGODB_URI=mongodb://mongodb:27017/your-database-name
    depends_on:
      - mongodbRun:
docker-compose up| Method | Endpoint | Description | 
|---|---|---|
| GET | /api/v1/persons | Fetch all persons | 
| POST | /api/v1/person | Create a new person | 
| PUT | /api/v1/person | Update an existing one | 
| DELETE | /api/v1/person | Delete a person | 
Use a tool like Postman or curl to test the endpoints:
curl -X GET http://localhost:8080/api/v1/persons- 
MongoDB connection error: - Ensure MongoDB is running and accessible at the specified MONGO_URI.
 
- Ensure MongoDB is running and accessible at the specified 
- 
Port already in use: - Stop any application running on port 8080or change the exposed port.
 
- Stop any application running on port