A beginner-friendly REST API built with Spring Boot and H2 in-memory database to perform CRUD operations on Department data.
📚 Built while learning Spring Boot from Daily Code Buffer's Spring Boot Master Class
| Technology | Version | Purpose |
|---|---|---|
| Java | 23 | Programming Language |
| Spring Boot | 4.0.3 | Backend Framework |
| Spring Data JPA | 4.0.3 | Database ORM |
| H2 Database | 2.4.240 | In-memory Database |
| Hibernate | 7.2.4 | ORM Implementation |
| Lombok | 1.18.42 | Reduce boilerplate code |
| Maven | - | Build Tool |
src/main/java/com/example/
├── controller/
│ ├── DepartmentController.java ← Handles HTTP requests
│ └── HelloController.java
├── service/
│ ├── DepartmentService.java ← Service interface
│ └── DepartmentServiceImpt.java ← Business logic
├── Repository/
│ └── DepartmentRepository.java ← Database layer
├── entity/
│ └── Department.java ← Database entity
└── SpringBootTutorialApplication.java
- Java 17+
- Maven
- IntelliJ IDEA (recommended)
- Postman (for API testing)
git clone https://github.com/Karan-Rastogi/Basic-Department--Api.git
cd Basic-Department--Apimvn spring-boot:runThe server will start at: http://localhost:8082
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
server.port=8082Access the database UI at:
http://localhost:8082/h2-console
| Field | Value |
|---|---|
| JDBC URL | jdbc:h2:mem:testdb |
| Username | sa |
| Password | (leave empty) |
@Entity
@Data
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long departmentId;
private String departmentName;
private String departmentAddress;
private String departmentCode;
}| Method | Endpoint | Description |
|---|---|---|
GET |
/departments |
Get all departments |
GET |
/departments/{id} |
Get department by ID |
GET |
/departments/name/{name} |
Get department by name |
POST |
/departments |
Create new department |
PUT |
/departments/{id} |
Update department |
DELETE |
/departments/{id} |
Delete department |
POST http://localhost:8082/departments
Content-Type: application/json
{
"departmentName": "Computer Science",
"departmentAddress": "Delhi",
"departmentCode": "CST"
}
GET http://localhost:8082/departments
GET http://localhost:8082/departments/1
GET http://localhost:8082/departments/name/Computer Science
PUT http://localhost:8082/departments/1
Content-Type: application/json
{
"departmentName": "Information Technology",
"departmentAddress": "Mumbai",
"departmentCode": "IT"
}
DELETE http://localhost:8082/departments/1
| Layer | File | Responsibility |
|---|---|---|
| Controller | DepartmentController.java |
Handles HTTP requests & responses |
| Service | DepartmentServiceImpt.java |
Contains business logic |
| Repository | DepartmentRepository.java |
Talks to H2 database |
| Entity | Department.java |
Maps to database table |
- Spring Boot project structure (Controller → Service → Repository → Entity)
- How REST APIs work with HTTP methods (GET, POST, PUT, DELETE)
- Connecting Spring Boot with H2 in-memory database
- Using Spring Data JPA and Hibernate ORM
- Using Lombok to reduce boilerplate code
- Testing APIs with Postman
- Version control with Git and GitHub
- Migrate from H2 to MySQL database
- Add exception handling with
@ControllerAdvice - Add input validation with
@Valid - Add pagination and sorting
- Add unit tests with JUnit and Mockito
- Add Spring Security with JWT authentication
Karan Rastogi
- GitHub: @Karan-Rastogi
⭐ If you found this helpful, give it a star!