A comprehensive, production-ready RESTful API for managing employee data, built with Spring Boot and implementing enterprise-grade security, validation, and documentation features.
- Full CRUD Operations - Create, Read, Update, and Delete employee records
- Role-Based Access Control (RBAC) - Secure endpoints with EMPLOYEE, MANAGER, and ADMIN roles
- HTTP Basic Authentication - Secure API access with database-backed user management
- Request Validation - Comprehensive input validation using Jakarta Bean Validation
- API Documentation - Interactive Swagger/OpenAPI documentation
- Database Integration - JPA/Hibernate with H2 in-memory database
- Custom Security Configuration - Flexible authentication with custom database schema support
- RESTful Design - Follows REST principles with proper HTTP status codes
- Exception Handling - Custom authentication entry point with JSON error responses
- Spring Boot 4.0.2 - Latest Spring Boot framework for rapid application development
- Java 17 - Modern Java with latest language features
- Maven - Dependency management and build automation
- Spring Web MVC - RESTful web services and MVC architecture
- Spring Data JPA - Data persistence layer with JPA/Hibernate
- Spring Security - Authentication and authorization framework
- Spring Boot DevTools - Development-time productivity tools
- Spring Boot Validation - Jakarta Bean Validation integration
- H2 Database - In-memory relational database for development
- Hibernate/JPA - Object-Relational Mapping (ORM) framework
- JPA Repository Pattern - Data access abstraction layer
- SpringDoc OpenAPI 3.0.1 - OpenAPI 3.0 specification and Swagger UI integration
- Swagger UI - Interactive API documentation and testing interface
- Spring Security - Enterprise security framework
- JdbcUserDetailsManager - Database-backed user authentication
- Jakarta Bean Validation - Input validation annotations (@NotBlank, @Email, @Size, @Min)
- Custom Authentication Entry Point - JSON-based error responses
- Spring Boot Test - Testing framework integration
- JUnit 5 - Unit and integration testing
All endpoints are prefixed with /api/employees and require authentication.
| Method | Endpoint | Description | Required Role |
|---|---|---|---|
GET |
/api/employees |
Get all employees | EMPLOYEE |
GET |
/api/employees/{id} |
Get employee by ID | EMPLOYEE |
POST |
/api/employees |
Create new employee | MANAGER |
PUT |
/api/employees/{id} |
Update employee | MANAGER |
DELETE |
/api/employees/{id} |
Delete employee | ADMIN |
/docs- Swagger UI documentation/h2-console- H2 Database Console
- EMPLOYEE Role: Read-only access (GET operations)
- MANAGER Role: Read and write access (GET, POST, PUT operations)
- ADMIN Role: Full access including delete operations (GET, POST, PUT, DELETE)
- HTTP Basic Authentication - Standard HTTP authentication mechanism
- Database-Backed User Management - Custom
system_usersandrolestables - Custom SQL Queries - Flexible user and authority lookup queries
- Custom Authentication Entry Point - JSON error responses for unauthorized access
This project follows layered architecture principles:
βββββββββββββββββββββββββββββββββββββββ
β Controller Layer (REST) β
β EmployeeRestController.java β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Service Layer β
β EmployeeService/ServiceImpl β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Repository Layer (DAO) β
β EmployeeRepository (JPA) β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Entity Layer β
β Employee (JPA Entity) β
βββββββββββββββββββββββββββββββββββββββ
- Repository Pattern - Data access abstraction
- Service Layer Pattern - Business logic separation
- DTO Pattern - Request/Response objects (EmployeeRequest)
- Dependency Injection - Constructor-based injection
- Bean Configuration - Java-based configuration classes
employees/
βββ src/
β βββ main/
β β βββ java/
β β β βββ com/employeeProject/employees/
β β β βββ controller/ # REST Controllers
β β β β βββ EmployeeRestController.java
β β β βββ service/ # Business Logic Layer
β β β β βββ EmployeeService.java
β β β β βββ EmployeeServiceImpl.java
β β β βββ dao/ # Data Access Layer
β β β β βββ EmployeeRepository.java
β β β βββ entity/ # JPA Entities
β β β β βββ Employee.java
β β β βββ request/ # DTOs/Request Objects
β β β β βββ EmployeeRequest.java
β β β βββ security/ # Security Configuration
β β β β βββ SecurityConfig.java
β β β β βββ SwaggerConfig.java
β β β βββ EmployeesApplication.java
β β βββ resources/
β β βββ application.properties.example
β β βββ application.properties
β βββ test/
β βββ java/
β βββ com/employeeProject/employees/
β βββ EmployeesApplicationTests.java
βββ pom.xml # Maven Configuration
βββ README.md
- Java 17 or higher
- Maven 3.6+
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
-
Clone the repository
git clone <repository-url> cd employees
-
Configure application properties
cp src/main/resources/application.properties.example src/main/resources/application.properties
Edit
application.propertiesand set your database password. -
Build the project
mvn clean install
-
Run the application
mvn spring-boot:run
Or run the
EmployeesApplicationclass directly from your IDE. -
Access the application
- API Base URL:
http://localhost:8080/api/employees - Swagger UI:
http://localhost:8080/docs - H2 Console:
http://localhost:8080/h2-console
- API Base URL:
The application uses H2 in-memory database. To use custom user authentication tables, create the following schema:
CREATE TABLE system_users (
user_id VARCHAR(50) NOT NULL,
password CHAR(68) NOT NULL,
active BOOLEAN NOT NULL,
PRIMARY KEY (user_id)
);
CREATE TABLE roles (
user_id VARCHAR(50) NOT NULL,
role VARCHAR(50) NOT NULL,
UNIQUE (user_id, role),
FOREIGN KEY (user_id) REFERENCES system_users (user_id)
);Run tests using Maven:
mvn testOnce the application is running, access the interactive Swagger UI at:
- URL:
http://localhost:8080/docs
The Swagger UI provides:
- Complete API endpoint documentation
- Request/Response schemas
- Try-it-out functionality
- Authentication support (Basic Auth)
Key configuration options in application.properties:
- Database connection settings
- H2 console configuration
- JPA/Hibernate settings
- Swagger UI path configuration
Security settings are configured in SecurityConfig.java:
- Role-based endpoint access
- HTTP Basic Authentication
- Custom authentication entry point
- β Modern Java Development - Java 17 with latest Spring Boot 4.0.2
- β Enterprise Security - Role-based access control with database authentication
- β RESTful Best Practices - Proper HTTP methods, status codes, and resource naming
- β Input Validation - Comprehensive validation with meaningful error messages
- β API Documentation - Auto-generated OpenAPI/Swagger documentation
- β Clean Architecture - Separation of concerns with layered architecture
- β Production-Ready - Exception handling, transaction management, and security
- β Developer Experience - DevTools, H2 console, and comprehensive documentation
Built with β€οΈ using Spring Boot