A complete Spring Boot project demonstrating REST API concepts, perfect for interview preparation.
This project implements a Book Management System with complete CRUD operations using Spring Boot.
- @RestController: Used in
bookcontroller.java
for handling REST requests - HTTP Methods Implementation:
- GET: Fetch books
- POST: Add new books
- PUT: Update books
- DELETE: Remove books
bootrestbook/
├── controllers/
│ └── bookcontroller.java # Handles HTTP requests
├── services/
│ └── BookService.java # Business logic
└── entities/
└── Book.java # Data model
@RestController
: Marks class as REST controller@GetMapping
: Handles GET requests@PostMapping
: Handles POST requests@PutMapping
: Handles PUT requests@DeleteMapping
: Handles DELETE requests@RequestBody
: Converts JSON to Java objects@PathVariable
: Extracts values from URL
- Each endpoint follows REST conventions
- Proper HTTP methods for CRUD operations
- Meaningful URL patterns (
/books
,/books/{id}
)
- Separation of concerns
- Business logic in service layer
- Controller only handles request/response
- Client sends HTTP request
- Controller receives request
- Service processes data
- Response returned to client
@Controller
: Traditional MVC controller, returns view names@RestController
: Combines@Controller
and@ResponseBody
, returns data directly
- Converts JSON/XML from request body to Java objects
- Used in POST/PUT methods to receive data
- Requires proper getters/setters in entity class
- Separates business logic from controllers
- Enables code reusability
- Makes testing easier
- Follows Single Responsibility Principle
-
Create (POST):
@PostMapping("/books") public Book addBook(@RequestBody Book book)
-
Read (GET):
@GetMapping("/books") public List<Book> getBooks()
-
Update (PUT):
@PutMapping("/books/{bookId}") public Book updateBook(@RequestBody Book book)
-
Delete (DELETE):
@DeleteMapping("/books/{bookId}") public void deleteBook(@PathVariable("bookId") int bookId)
- Adding JPA dependencies
- Configuring MySQL database
- Creating proper entity mappings
- Implementing JPA Repository
- Proper package structure
- Service layer implementation
- Clear naming conventions
- Separation of concerns
- Clone the repository
- Run
mvn spring-boot:run
- Access endpoints at
http://localhost:8080/books
Use Postman to test:
- GET
http://localhost:8080/books
- POST
http://localhost:8080/books
with JSON body - PUT
http://localhost:8080/books/{id}
- DELETE
http://localhost:8080/books/{id}