Skip to content

Product API Implementation

Overview

This document describes the implementation of REST API endpoints for the Product entity as specified in step 5 of the prompt_pad.md file.

Implementation Details

1. ProductService Enhancements

File: /src/main/java/com/anr/service/ProductService.java

Added the following methods to support full CRUD operations: - findAll() - Retrieves all products - updateProduct(String id, Product product) - Updates an existing product - deleteProduct(String id) - Deletes a product by ID

2. ProductController

File: /src/main/java/com/anr/controller/ProductController.java

Created a new REST controller with the following endpoints:

Endpoints

Method Endpoint Description Response Code
GET /api/v1/products List all products 200 OK
GET /api/v1/products/{id} Get product by ID 200 OK, 404 Not Found
POST /api/v1/products Create new product 201 Created, 400 Bad Request
PUT /api/v1/products/{id} Update existing product 200 OK, 404 Not Found
DELETE /api/v1/products/{id} Delete product 204 No Content, 404 Not Found

3. Features Implemented

REST Best Practices

  • Proper HTTP methods (GET, POST, PUT, DELETE)
  • Appropriate HTTP status codes (200, 201, 204, 400, 404, 500)
  • JSON request/response format
  • RESTful URL structure

OpenAPI/Swagger Documentation

  • Complete API documentation with @Operation annotations
  • Detailed @ApiResponses for all endpoints
  • Request/response schema definitions
  • Tagged with "Product Management" for better organization

Error Handling

  • Returns 404 when product not found
  • Returns 400 for invalid input
  • Uses Product.EMPTY pattern for not-found scenarios

Spring Boot Integration

  • Uses @RestController and @RequestMapping
  • Dependency injection with @Autowired
  • Proper media type declarations (APPLICATION_JSON)
  • Follows existing controller patterns in the application

Testing the API

Using Swagger UI

Access the Swagger UI at: http://localhost:8080/swagger-ui.html

All Product endpoints will be available under the "Product Management" section.

Sample cURL Commands

1. List all products

curl -X GET http://localhost:8080/api/v1/products

2. Get product by ID

curl -X GET http://localhost:8080/api/v1/products/P001

3. Create a new product

curl -X POST http://localhost:8080/api/v1/products \
  -H "Content-Type: application/json" \
  -d '{
    "id": "P001",
    "name": "Sample Product",
    "description": "This is a sample product",
    "price": "99.99"
  }'

4. Update an existing product

curl -X PUT http://localhost:8080/api/v1/products/P001 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Updated Product",
    "description": "Updated description",
    "price": "149.99"
  }'

5. Delete a product

curl -X DELETE http://localhost:8080/api/v1/products/P001

Architecture Alignment

The implementation follows the existing application patterns: - Controller Layer: Handles HTTP requests/responses - Service Layer: Contains business logic - Repository Layer: Data access (already existed) - Model Layer: Entity definitions (already existed)

Quality Attributes

✅ Follows Spring Boot 3.x conventions
✅ Uses Jakarta EE annotations (not javax)
✅ Comprehensive OpenAPI documentation
✅ RESTful design principles
✅ Proper HTTP status codes
✅ Consistent with existing codebase style
✅ Build successful (verified)

Next Steps

To fully test the implementation: 1. Start the application: gradle bootRun 2. Access Swagger UI: http://localhost:8080/swagger-ui.html 3. Test endpoints using Swagger UI or cURL commands 4. Optionally add unit tests for the new controller 5. Optionally add integration tests


Implementation Date: October 22, 2025
Status: ✅ Complete