Skip to content

Karan-Rastogi/Basic-Department--Api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🏢 Basic Department API

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


🛠️ Tech Stack

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

📁 Project Structure

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

⚙️ Setup & Installation

Prerequisites

  • Java 17+
  • Maven
  • IntelliJ IDEA (recommended)
  • Postman (for API testing)

1. Clone the repository

git clone https://github.com/Karan-Rastogi/Basic-Department--Api.git
cd Basic-Department--Api

2. Run the project

mvn spring-boot:run

The server will start at: http://localhost:8082


⚙️ Configuration (application.properties)

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=8082

H2 Console

Access the database UI at:

http://localhost:8082/h2-console
Field Value
JDBC URL jdbc:h2:mem:testdb
Username sa
Password (leave empty)

🗂️ Department Entity

@Entity
@Data
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long departmentId;
    private String departmentName;
    private String departmentAddress;
    private String departmentCode;
}

🔗 API Endpoints

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

🧪 Testing with Postman

Save a Department

POST http://localhost:8082/departments
Content-Type: application/json

{
    "departmentName": "Computer Science",
    "departmentAddress": "Delhi",
    "departmentCode": "CST"
}

Get All Departments

GET http://localhost:8082/departments

Get Department by ID

GET http://localhost:8082/departments/1

Get Department by Name

GET http://localhost:8082/departments/name/Computer Science

Update Department

PUT http://localhost:8082/departments/1
Content-Type: application/json

{
    "departmentName": "Information Technology",
    "departmentAddress": "Mumbai",
    "departmentCode": "IT"
}

Delete Department

DELETE http://localhost:8082/departments/1

🗂️ Layer Explanation

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

📚 What I Learned

  • 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

🚀 Future Improvements

  • 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

👨‍💻 Author

Karan Rastogi


⭐ If you found this helpful, give it a star!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages