A complete Student Management System built with Spring Boot, demonstrating all CRUD (Create, Read, Update, Delete) operations with proper validation, error handling, and best practices.
- Complete CRUD Operations for Student management
- Input Validation with detailed error messages
- Global Exception Handling for consistent error responses
- Sample Data Initialization for testing
- H2 In-Memory Database for development
- RESTful API Design following best practices
- Comprehensive Logging for debugging
- Unit Tests for controllers
- Detailed Code Comments for beginners
src/
βββ main/
β βββ java/com/example/demo/
β β βββ DemoApplication.java # Main Spring Boot application class
β β βββ config/
β β β βββ DataInitializer.java # Sample data initialization
β β βββ controller/
β β β βββ StudentController.java # REST API endpoints
β β βββ dto/
β β β βββ StudentRequestDTO.java # Request data transfer object
β β β βββ StudentResponseDTO.java # Response data transfer object
β β βββ entity/
β β β βββ Student.java # JPA entity for database
β β βββ exception/
β β β βββ ErrorResponse.java # Error response structure
β β β βββ GlobalExceptionHandler.java # Global error handling
β β β βββ ValidationErrorResponse.java # Validation error structure
β β βββ repository/
β β β βββ StudentRepository.java # Data access layer
β β βββ service/
β β βββ StudentService.java # Business logic interface
β β βββ impl/
β β βββ StudentServiceImpl.java # Business logic implementation
β βββ resources/
β βββ application.properties # Application configuration
βββ test/
βββ java/com/example/demo/
βββ controller/
βββ StudentControllerTest.java # Unit tests for controller
- Java 17 - Programming language
- Spring Boot 3.5.7 - Framework
- Spring Data JPA - Database access
- Spring Boot Validation - Input validation
- H2 Database - In-memory database
- Lombok - Reduces boilerplate code
- Maven - Build tool
- JUnit 5 - Testing framework
- Java 17 or higher
- Maven (or use included Maven wrapper)
-
Clone or download the project
-
Navigate to project directory
cd demo -
Run the application
./mvnw spring-boot:run
-
Application will start on port 8080
http://localhost:8080 -
Access H2 Database Console (optional)
http://localhost:8080/h2-console- JDBC URL:
jdbc:h2:mem:studentdb - Username:
sa - Password: (leave empty)
- JDBC URL:
The application automatically creates 5 sample students:
- John Doe (Computer Science, Active)
- Jane Smith (Business Administration, Active)
- Mike Johnson (Engineering, Active)
- Emily Davis (Psychology, Inactive)
- David Wilson (Computer Science, Active)
Endpoint: POST /api/students
Request Body:
{
"firstName": "Alice",
"lastName": "Brown",
"email": "alice.brown@email.com",
"age": 21,
"course": "Mathematics",
"phoneNumber": "1111222233",
"dateOfBirth": "2002-06-15",
"address": "123 University Ave",
"isActive": true
}Response (201 Created):
{
"id": 6,
"firstName": "Alice",
"lastName": "Brown",
"fullName": "Alice Brown",
"email": "alice.brown@email.com",
"age": 21,
"course": "Mathematics",
"phoneNumber": "1111222233",
"dateOfBirth": "2002-06-15",
"address": "123 University Ave",
"isActive": true
}cURL Command:
curl -X POST http://localhost:8080/api/students \
-H "Content-Type: application/json" \
-d '{
"firstName": "Alice",
"lastName": "Brown",
"email": "alice.brown@email.com",
"age": 21,
"course": "Mathematics",
"phoneNumber": "1111222233",
"dateOfBirth": "2002-06-15",
"address": "123 University Ave",
"isActive": true
}'Endpoint: GET /api/students
Response (200 OK):
[
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"fullName": "John Doe",
"email": "john.doe@email.com",
"age": 20,
"course": "Computer Science",
"phoneNumber": "1234567890",
"dateOfBirth": "2003-05-15",
"address": "123 Main Street, City, State",
"isActive": true
}
// ... more students
]cURL Command:
curl -X GET http://localhost:8080/api/studentsEndpoint: GET /api/students/{id}
Response (200 OK):
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"fullName": "John Doe",
"email": "john.doe@email.com",
"age": 20,
"course": "Computer Science",
"phoneNumber": "1234567890",
"dateOfBirth": "2003-05-15",
"address": "123 Main Street, City, State",
"isActive": true
}cURL Command:
curl -X GET http://localhost:8080/api/students/1Endpoint: PUT /api/students/{id}
Request Body:
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe.updated@email.com",
"age": 21,
"course": "Computer Science",
"phoneNumber": "1234567890",
"dateOfBirth": "2003-05-15",
"address": "456 Updated Street",
"isActive": true
}Response (200 OK):
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"fullName": "John Doe",
"email": "john.doe.updated@email.com",
"age": 21,
"course": "Computer Science",
"phoneNumber": "1234567890",
"dateOfBirth": "2003-05-15",
"address": "456 Updated Street",
"isActive": true
}cURL Command:
curl -X PUT http://localhost:8080/api/students/1 \
-H "Content-Type: application/json" \
-d '{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe.updated@email.com",
"age": 21,
"course": "Computer Science",
"phoneNumber": "1234567890",
"dateOfBirth": "2003-05-15",
"address": "456 Updated Street",
"isActive": true
}'Endpoint: DELETE /api/students/{id}
Response: 204 No Content
cURL Command:
curl -X DELETE http://localhost:8080/api/students/1curl -X GET http://localhost:8080/api/students/email/john.doe@email.comcurl -X GET http://localhost:8080/api/students/course/Computer%20Sciencecurl -X GET http://localhost:8080/api/students/activecurl -X GET http://localhost:8080/api/students/inactivecurl -X GET "http://localhost:8080/api/students/search?name=John"curl -X GET "http://localhost:8080/api/students/age-range?minAge=18&maxAge=25"curl -X PATCH "http://localhost:8080/api/students/1/status?isActive=false"curl -X GET "http://localhost:8080/api/students/email-exists?email=john.doe@email.com"curl -X GET http://localhost:8080/api/students/course/Computer%20Science/countcurl -X GET http://localhost:8080/api/students/active/countThe application provides detailed error responses for various scenarios:
{
"timestamp": "2025-11-03T10:30:00",
"status": 400,
"error": "Validation Failed",
"message": "Input validation failed",
"validationErrors": {
"firstName": "First name is required",
"email": "Please provide a valid email address",
"age": "Age must be at least 15"
},
"path": "/api/students"
}{
"timestamp": "2025-11-03T10:30:00",
"status": 400,
"error": "Bad Request",
"message": "Student not found with ID: 999",
"path": "/api/students"
}{
"timestamp": "2025-11-03T10:30:00",
"status": 400,
"error": "Bad Request",
"message": "Student with email john.doe@email.com already exists",
"path": "/api/students"
}./mvnw test./mvnw test -Dtest=StudentControllerTestThis project demonstrates key concepts for beginners:
- Spring Boot Basics - Auto-configuration, annotations, dependency injection
- REST API Development - HTTP methods, status codes, request/response handling
- Data Persistence - JPA entities, repositories, database operations
- Input Validation - Bean validation, custom validators
- Error Handling - Global exception handlers, custom error responses
- Testing - Unit tests, mocking, test-driven development
- Project Structure - Layered architecture, separation of concerns
- Best Practices - Code organization, naming conventions, documentation
- Controller Layer - Handles HTTP requests/responses
- Service Layer - Contains business logic
- Repository Layer - Handles data access
- Entity Layer - Represents database tables
- RequestDTO - Data coming from client
- ResponseDTO - Data going to client
- Separates API structure from database structure
- Spring automatically provides required dependencies
@Autowired,@RequiredArgsConstructorhandle injection
- Automatic CRUD operations
- Custom query methods
- No implementation needed (Spring provides it)
@NotBlank,@NotNull,@Email,@Min,@Max- Automatic validation with
@Valid
- Add pagination and sorting
- Implement security with JWT
- Add file upload for student photos
- Create a frontend with React/Angular
- Add email notifications
- Implement audit logging
- Add Docker containerization
Feel free to submit issues and enhancement requests!
This project is for educational purposes.