A modern REST API for employee management built with Spring Boot 3.2.12 and Java 21.
- Full CRUD Operations - Create, Read, Update, Delete employees
- Input Validation - Jakarta Bean Validation with detailed error messages
- Filter by Status - Query employees by their status
- Global Exception Handling - Structured error responses
- MySQL Database - JPA/Hibernate with auto-schema generation
- CORS Support - Configured for cross-origin requests
- Hot Reload - Spring Boot DevTools for development
| Technology | Version |
|---|---|
| Java | 21 (LTS) |
| Spring Boot | 3.2.12 |
| Jakarta EE | 3.x |
| Hibernate | 6.4.10 |
| MySQL | 8.x |
| Maven | 3.9.x |
This project was recently upgraded from Java 17 to Java 21 with the following improvements:
β
Java 21 LTS - Latest long-term support version
β
Spring Boot 3.2.12 - Modern Spring Framework 6.x
β
Jakarta EE Migration - Migrated from javax.* to jakarta.*
β
Enhanced Validation - Bean validation with @Valid and @NonNull
β
Improved Error Handling - Global exception handler with structured responses
β
Type Safety - LocalDate for dates, wrapper types for optional fields
β
Better REST API - ResponseEntity with proper HTTP status codes
Before running this application, ensure you have:
- JDK 21 or later installed
- MySQL 8.x running on
localhost:3306 - Maven 3.9.x (or use included Maven wrapper)
- Database:
emp_db
git clone https://github.com/Palenzo/Java-Employee-Management-Website.git
cd Java-Employee-Management-WebsiteCREATE DATABASE IF NOT EXISTS emp_db;Or using command line:
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS emp_db;"Edit src/main/resources/application.properties if needed:
spring.datasource.url=jdbc:mysql://localhost:3306/emp_db
spring.datasource.username=root
spring.datasource.password=YOUR_PASSWORDUsing Maven Wrapper (Recommended):
./mvnw spring-boot:runOr build and run JAR:
./mvnw clean package
java -jar target/crud-0.0.1-SNAPSHOT.jarThe application will start on http://localhost:8080
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
| POST | /api/employee |
Create new employee | JSON (see below) |
| GET | /api/employee |
Get all employees | - |
| GET | /api/employee?status=1 |
Filter by status | - |
| GET | /api/employee/{id} |
Get employee by ID | - |
| PUT | /api/employee/{id} |
Update employee | JSON (partial) |
| DELETE | /api/employee/{id} |
Delete employee | - |
curl -X POST http://localhost:8080/api/employee \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"doj": "2025-01-15",
"status": 1,
"salary": 50000.0
}'Response (201 Created):
{
"id": 1,
"name": "John Doe",
"doj": "2025-01-15",
"status": 1,
"salary": 50000.0
}curl http://localhost:8080/api/employeeResponse (200 OK):
[
{
"id": 1,
"name": "John Doe",
"doj": "2025-01-15",
"status": 1,
"salary": 50000.0
}
]curl http://localhost:8080/api/employee/1curl -X PUT http://localhost:8080/api/employee/1 \
-H "Content-Type: application/json" \
-d '{
"salary": 55000.0
}'curl -X DELETE http://localhost:8080/api/employee/1Response (200 OK):
{
"message": "Employee deleted successfully"
}Invoke-RestMethod -Uri "http://localhost:8080/api/employee" `
-Method Post `
-ContentType "application/json" `
-Body '{"name":"Jane Smith","doj":"2025-10-16","status":1,"salary":60000.0}'Invoke-RestMethod -Uri "http://localhost:8080/api/employee" -Method GetInvoke-RestMethod -Uri "http://localhost:8080/api/employee?status=1" -Method GetInvoke-RestMethod -Uri "http://localhost:8080/api/employee/1" `
-Method Put `
-ContentType "application/json" `
-Body '{"salary":65000.0}'Invoke-RestMethod -Uri "http://localhost:8080/api/employee/1" -Method DeleteThe API validates incoming requests:
| Field | Rule | Error Message |
|---|---|---|
| name | Required, not blank | "name is required" |
| doj | Not in future | "date of joining cannot be in the future" |
| salary | Optional | - |
| status | Optional | - |
Validation Error Response (400 Bad Request):
{
"name": "name is required",
"doj": "date of joining cannot be in the future"
}src/
βββ main/
β βββ java/com/employee/crud/
β β βββ CrudApplication.java # Main application
β β βββ controller/
β β β βββ EmployeeController.java # REST endpoints
β β β βββ GlobalExceptionHandler.java # Error handling
β β β βββ WebConfig.java # CORS configuration
β β βββ entity/
β β β βββ Employee.java # JPA entity
β β βββ repository/
β β β βββ EmployeeRepository.java # Data access
β β βββ service/
β β βββ EmployeeService.java # Service interface
β β βββ EmployeeServiceImpl.java # Business logic
β βββ resources/
β βββ application.properties # Configuration
βββ test/
βββ java/com/employee/crud/
βββ CrudApplicationTests.java # Unit tests
Error: Communications link failure
Solution:
- Verify MySQL is running:
Get-Service -Name "*mysql*" - Start MySQL:
Start-Service MySQL - Check credentials in
application.properties - Ensure database
emp_dbexists
Error: Port 8080 is already in use
Solution:
# Find process using port 8080
netstat -ano | findstr :8080
# Kill the process (replace PID)
Stop-Process -Id <PID> -ForceError: Unsupported class file major version
Solution: Ensure you're using JDK 21 or later:
java -versionIf using an older JDK, install JDK 21 and set JAVA_HOME:
$env:JAVA_HOME = 'C:\Program Files\Java\jdk-21'./mvnw test./mvnw clean package -DskipTestsThe project includes Spring Boot DevTools. Changes to Java files will auto-reload during development.
Hibernate auto-generates the schema (spring.jpa.hibernate.ddl-auto=update). The employee table structure:
| Column | Type | Constraints |
|---|---|---|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
| name | VARCHAR(255) | NOT NULL |
| doj | DATE | - |
| status | INT | - |
| salary | FLOAT | - |
CORS is enabled for:
- Origin:
http://localhost(configurable inWebConfig.java) - Methods: GET, POST, PUT, DELETE, OPTIONS
- Headers: All
- Credentials: Enabled
Key dependencies:
spring-boot-starter-web- REST API supportspring-boot-starter-data-jpa- JPA/Hibernatespring-boot-starter-validation- Bean validationmysql-connector-j- MySQL driverspring-boot-devtools- Hot reloadspring-boot-starter-test- Testing support
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
Palenzo
- GitHub: @Palenzo
- Repository: Java-Employee-Management-Website
If you encounter any issues or have questions:
- Check the Troubleshooting section
- Open an issue on GitHub
- Review existing issues for solutions
Built with β€οΈ using Java 21 and Spring Boot 3.2.12