This document describes a Spring Boot REST controller for managing categories, providing endpoints for CRUD operations including fetching all categories, child categories, creating, updating, and deleting categories. It utilizes Swagger for API documentation and JWT for security, along with a Circuit Breaker for handling failures when fetching categories by ID. The controller delegates business logic to a CategoryService and uses Lombok for logging purposes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views4 pages
New Microsoft Word Document
This document describes a Spring Boot REST controller for managing categories, providing endpoints for CRUD operations including fetching all categories, child categories, creating, updating, and deleting categories. It utilizes Swagger for API documentation and JWT for security, along with a Circuit Breaker for handling failures when fetching categories by ID. The controller delegates business logic to a CategoryService and uses Lombok for logging purposes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
// This is a REST controller in Spring Boot.
It exposes HTTP endpoints for Category operations. @RestController
// Lombok annotation to enable logging in the class
using 'log' variable. @Slf4j public class CategoryResource {
// Injecting the CategoryService to delegate
business logic to service layer. private CategoryService catService;
// Constructor-based injection of CategoryService.
public CategoryResource(CategoryService catService) { this.catService = catService; }
// ========== API 1: GET all categories
========== // Adds Swagger summary and enables JWT bearer token protection for Swagger UI. @Operation(summary = "findAllCategories", security = @SecurityRequirement(name = "bearerAuth")) @GetMapping("/categories") // Maps GET request to /categories URL. public ResponseEntity<List<CategoryBean>> findAllCategories() { // Delegates the call to service layer and wraps result in HTTP 200 OK response. return ResponseEntity.ok(catService.findAllCategories()); }
// ========== API 2: GET child categories of a
parent ========== @Operation(summary = "findChildCategories", security = @SecurityRequirement(name = "bearerAuth")) @GetMapping("/categories/childs/{parentId}") // Dynamic URL to fetch child categories by parent ID. public ResponseEntity<List<CategoryBean>> findChildCategories(@PathVariable("parentId") int parentId) { // Calls service method to get all child categories of the specified parent. return ResponseEntity.ok(catService.findAllChilds(parentId)); }
// ========== API 3: CREATE a new category
========== @Operation(summary = "createCategory", security = @SecurityRequirement(name = "bearerAuth")) @PostMapping("/categories") // Maps POST requests to /categories. public ResponseEntity<CategoryBean> createCategory(@RequestBody CategoryBean catBean) { // Accepts category details from request body and sends it to service to save in DB. return ResponseEntity.ok(catService.createCategory(catBean)); }
// ========== API 4: UPDATE an existing
category ========== @Operation(summary = "updateCategory", security = @SecurityRequirement(name = "bearerAuth")) @PutMapping("/categories") // Maps PUT requests to /categories. public ResponseEntity<CategoryBean> updateCategory(@RequestBody CategoryBean catBean) { // Accepts updated category details from request body and delegates update operation to service. return ResponseEntity.ok(catService.updateCategory(catBean)) ; }
// ========== API 5: DELETE a category by ID
========== @Operation(summary = "deleteCategory", security = @SecurityRequirement(name = "bearerAuth")) @DeleteMapping("/categories/{catId}") // Dynamic URL for DELETE operation using category ID. public ResponseEntity<List<CategoryBean>> deleteCategory(@PathVariable("catId") int catId) { // Calls service to delete category and returns updated list of categories. return ResponseEntity.ok(catService.deleteCategory(catId)); }
// ========== API 6: GET category by ID with
CircuitBreaker ========== @Operation(summary = "getCategoryById", security = @SecurityRequirement(name = "bearerAuth")) @GetMapping("/categories/{catId}") // Dynamic GET URL to fetch a category by its ID. @CircuitBreaker(name = "categories-circuit") // Resilience4j Circuit Breaker used to handle failures. public ResponseEntity<CategoryBean> getCategoryById(@PathVariable("catId") int catId) { // Logging the access of this endpoint for debug purposes. log.info("get category by caegory id is invoking");
// Delegating the fetch operation to service and
returning the result in HTTP 200 response. return ResponseEntity.ok(catService.findCategoryById(catId)); } }