A beginner-friendly REST API project built with Spring Boot and MongoDB, covering User Registration, Login, and Logout functionality.
| Technology | Purpose |
|---|---|
| Java | Programming Language |
| Spring Boot | Backend Framework |
| MongoDB | NoSQL Database |
| Lombok | Reduces boilerplate code |
| Postman | API Testing |
src/main/java/com.example.demo/
├── controller/
│ └── AuthController.java
├── service/
│ └── UserService.java
├── repository/
│ └── UserRepository.java
├── entity/
│ └── Users.java
├── dto/
│ ├── LoginRequest.java
│ └── RegisterRequest.java
└── DemoApplication.java
- Java 17+
- Maven
- MongoDB running locally
- Postman (for testing)
git clone https://github.com/yourusername/student-auth-api.git
cd student-auth-apispring.data.mongodb.uri=mongodb://localhost:27017/studentdb
spring.data.mongodb.database=studentdbmvn spring-boot:runThe server will start at: http://localhost:8080
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>| Method | Endpoint | Description |
|---|---|---|
POST |
/api/auth/register |
Register a new user |
POST |
/api/auth/login |
Login user |
POST |
/api/auth/logout |
Logout user |
POST http://localhost:8080/api/auth/register
Content-Type: application/json
{
"userName": "rahul123",
"password": "mypassword"
}
Response:
User registered successfully!
POST http://localhost:8080/api/auth/login
Content-Type: application/json
{
"userName": "rahul123",
"password": "mypassword"
}
Response:
Login successful! Welcome rahul123
POST http://localhost:8080/api/auth/logout
Content-Type: application/json
{
"userName": "rahul123"
}
Response:
Logout successful! Bye rahul123
| Layer | File | Responsibility |
|---|---|---|
| Controller | AuthController.java |
Handles HTTP requests & responses |
| Service | UserService.java |
Contains business logic |
| Repository | UserRepository.java |
Talks to MongoDB database |
| Entity | Users.java |
Maps to MongoDB collection |
| DTO | LoginRequest.java, RegisterRequest.java |
Receives data from client |
- Passwords are stored as plain text (not encrypted)
- No JWT token-based authentication
- No role-based access control
- Add BCrypt password encryption
- Implement JWT Token authentication
- Add Spring Security
- Add Student CRUD APIs
- Add proper error handling with custom exceptions
- Spring Boot project structure (Controller, Service, Repository, Entity, DTO)
- How REST APIs work (GET, POST, PUT, DELETE)
- Connecting Spring Boot with MongoDB
- Using Lombok to reduce boilerplate
- Testing APIs with Postman
Made with ❤️ while learning Spring Boot from Engineering Digest