The backend of the Employee Management System is built using Spring Boot, a powerful Java framework designed to simplify the development of production-ready applications. This backend provides a RESTful API for managing employee and department data, connecting to both MySQL and MongoDB databases.
- RESTful API: Provides endpoints for CRUD operations on employees and departments.
- Data Initialization: Includes sample data for departments and employees.
- Integration: Connects to both MySQL and MongoDB databases.
- Exception Handling: Custom error handling for not found resources.
- Spring Boot: Framework for building production-ready applications with Java.
- MySQL: Relational database for structured data storage.
- MongoDB: NoSQL database for non-relational data storage.
employee-management-app
β
βββ docker-compose.yaml
β
βββ backend
β βββ src
β β βββ main
β β β βββ java
β β β β βββ com
β β β β βββ example
β β β β βββ employeemanagement
β β β β βββ EmployeeManagementApplication.java
β β β β βββ config
β β β β β βββ CorsConfig.java
β β β β β βββ DataInitializer.java
β β β β βββ controller
β β β β β βββ DepartmentController.java
β β β β β βββ EmployeeController.java
β β β β βββ model
β β β β β βββ Department.java
β β β β β βββ Employee.java
β β β β βββ repository
β β β β β βββ DepartmentRepository.java
β β β β β βββ EmployeeRepository.java
β β β β βββ service
β β β β β βββ DataInitializer.java
β β β β βββ exception
β β β β βββ ResourceNotFoundException.java
β β β βββ resources
β β β βββ application.properties
β β β βββ data.sql
β β βββ test
β β βββ java
β β βββ com
β β βββ example
β β βββ employeemanagement
β β βββ EmployeeManagementApplicationTests.java
β βββ .gitignore
β βββ pom.xml
β βββ compose.yaml
β
βββ frontend
βββ (frontend code)
git clone https://github.com/hoangsonww/Employee-Management-Fullstack-App.git
cd Employee-Management/backend
Ensure you have Maven and Java JDK installed. Run the following command to install the required dependencies:
mvn install -DskipTests
Update src/main/resources/application.properties
with your database configuration:
# MySQL Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/employee_management
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
# MongoDB Configuration
spring.data.mongodb.uri=mongodb://localhost:27017/employee_management
Ensure the databases are set up as expected and the URLs, usernames, and passwords match your local or remote database setup.
Run the following command to start the Spring Boot application:
mvn spring-boot:run
The backend server will be available at http://localhost:8080.
Here are some example API endpoints you can use to interact with the backend:
-
Get All Employees:
curl -X GET http://localhost:8080/api/employees
-
Get Employee by ID:
curl -X GET http://localhost:8080/api/employees/1
-
Create a New Employee:
curl -X POST http://localhost:8080/api/employees -H "Content-Type: application/json" -d '{"firstName": "John", "lastName": "Doe", "email": "[email protected]", "departmentId": 1}'
-
Update an Employee:
curl -X PUT http://localhost:8080/api/employees/1 -H "Content-Type: application/json" -d '{"firstName": "John", "lastName": "Doe", "email": "[email protected]", "departmentId": 1}'
-
Delete an Employee:
curl -X DELETE http://localhost:8080/api/employees/1
-
Get All Departments:
curl -X GET http://localhost:8080/api/departments
-
Get Department by ID:
curl -X GET http://localhost:8080/api/departments/1
The DataInitializer.java
class is used to preload sample data into the database. This is particularly useful for development and testing.
To run the unit and integration tests, use:
mvn test
The main class that serves as the entry point for the Spring Boot application.
REST controllers for handling HTTP requests related to departments and employees, respectively.
Entity classes representing the departments
and employees
tables in the MySQL database.
Repository interfaces for performing CRUD operations on the departments
and employees
entities.
A service class that initializes the database with sample data upon application startup.
A custom exception class used for handling cases where requested resources are not found.
Configuration file for Spring Boot, including database connection settings.
SQL script for preloading sample data into the MySQL database.
The backend API is documented using Swagger, which provides a user-friendly interface for exploring the available endpoints. To access the Swagger UI, navigate to http://localhost:8080/swagger-ui.html after starting the backend server.
If you have everything set up correctly, you should see the Swagger UI with a list of available endpoints and the ability to test them directly from the browser:
-
Could not autowire
Errors: Ensure all Spring Boot components (controllers, services, repositories) are correctly annotated and located in the appropriate package structure. -
Exception opening socket
for MongoDB: Verify that MongoDB is running and accessible atlocalhost:27017
. Check MongoDB logs for connection issues. -
SQLSyntaxErrorException
: Check thedata.sql
script and ensure the MySQL database schema matches the expected structure. -
Port Already in Use
: If the default port8080
is already in use, change the port inapplication.properties
or terminate the conflicting process. -
CORS Error
: If you encounter CORS issues, ensure that theCorsConfig.java
class is correctly configured. -
Build failed
: If the Maven build fails, check if you are using Java 11 and have the necessary dependencies installed. Also, check the error logs for more details.
If you'd like to contribute to the backend development, please fork the repository and submit a pull request with your changes. Ensure that you follow the project's coding standards and include relevant tests for new features.
This project is licensed under the MIT License. See the LICENSE file for details.
For more information about this project, please refer to the comprehensive documentation.
For any questions or issues, please contact [email protected].