Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

thusithz/spring-boot-with-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

Spring Boot Application with MongoDB and Docker

This repository contains a Spring Boot application that connects to a MongoDB database and includes Docker support for easy deployment.


πŸš€ Features

  • RESTful APIs built with Spring Boot.
  • MongoDB as the database for persistence.
  • Docker support for containerized deployment.
  • Easy configuration with environment variables.

πŸ›  Prerequisites

Before running the application, ensure you have the following installed:

  1. Java Development Kit (JDK): Version 17 or later.
  2. Maven: For building the project.
  3. Docker: For containerization.
  4. MongoDB: If running locally.

πŸ“‚ Project Structure

.
β”œβ”€β”€ 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

βš™οΈ Configuration

1. MongoDB Setup

Option 1: Local MongoDB

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-name

Option 2: MongoDB Docker Container

Run MongoDB using Docker:

docker run -d -p 27017:27017 --name mongodb mongo

2. Environment Variables

You can set environment variables to configure the application:

  • SPRING_PROFILES_ACTIVE: Set the Spring profile (e.g., dev, prod).
  • MONGO_URI: MongoDB connection string.

πŸ—οΈ Build and Run

1. Clone the Repository

git clone https://github.com/thusithz/spring-boot-with-mongodb.git
cd spring-boot-with-mongodb

2. Build the Application

Use Maven to build the application:

mvn clean package

3. Run the Application Locally

Run the application with Maven:

mvn spring-boot:run

🐳 Docker Instructions

1. Build Docker Image

docker build -t spring-boot-app .

2. Run Docker Container

Run the Spring Boot application:

docker run -p 8080:8080 --name spring-boot-app spring-boot-app

3. Run MongoDB and Spring Boot Together

Use 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:
 - mongodb

Run:

docker-compose up

πŸ“‹ API Endpoints

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

πŸ” Testing the API

Use a tool like Postman or curl to test the endpoints:

curl -X GET http://localhost:8080/api/v1/persons

πŸ›  Troubleshooting

  1. MongoDB connection error:

    • Ensure MongoDB is running and accessible at the specified MONGO_URI.
  2. Port already in use:

    • Stop any application running on port 8080 or change the exposed port.

About

Spring Boot application that connects to a MongoDB database

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /