Error handling for Spring Boot APIs — @ControllerAdvice, structured error
84
75%
Does it follow best practices?
Impact
99%
1.76xAverage score across 5 eval scenarios
Passed
No known issues
A warehouse needs a REST API to track product inventory and stock movements. Build it using Spring Boot and Java.
Resources:
{ id, sku, name, description, category, unitPrice }{ productId, warehouseLocation, quantity, lastUpdated }{ id, productId, type (IN/OUT/ADJUSTMENT), quantity, reason, timestamp, performedBy }Endpoints:
GET /api/products -- list products, support filtering by category and search by namePOST /api/products -- create a product (sku required and must be unique, name required, unitPrice > 0)GET /api/products/{id} -- get product with current stock levelPUT /api/products/{id} -- update product details (cannot change SKU)GET /api/products/{id}/movements -- get stock movement history for a product, support paginationPOST /api/stock/receive -- receive stock (productId, quantity > 0, warehouseLocation required, performedBy required)POST /api/stock/dispatch -- dispatch stock (productId, quantity > 0, warehouseLocation required, performedBy required; cannot dispatch more than available stock)POST /api/stock/adjust -- adjust stock level (productId, quantity can be positive or negative, reason required, performedBy required; resulting stock cannot be negative)GET /api/stock/levels -- get current stock levels across all products and locationsGET /api/stock/low -- get products where stock is below a threshold (threshold query param, default 10)Business rules:
Use H2 embedded database with Spring Data JPA.
Produce a complete Spring Boot project:
src/main/java/com/example/ -- controller, service, entity, and repository packagespom.xml or build.gradle -- with dependenciessrc/main/resources/application.properties -- H2 and application configurationYou may create additional files as needed for a well-structured codebase.