Overview
The Products API provides endpoints for managing the product catalog, including CRUD operations, search, filtering, and image gallery management.
Base URL : /api/productos
Source : ProductController.java
Authentication
Public endpoints : GET requests (list, search, view)
Admin-only endpoints : POST, PUT, DELETE operations require ROLE_ADMIN
Endpoints
List All Products
Retrieve all products in the catalog (including inactive products).
Authentication : None (public endpoint)
Response : 200 OK
Array of product objects Category object with id and nombre
Array of image URL objects
Active status (soft delete)
URL-friendly product identifier
Example Request :
curl -X GET http://localhost:8080/api/productos
List Active Products
GET /api/productos/activos
Retrieve only active products (where activo = true).
Authentication : None (public endpoint)
Response : 200 OK - Array of active product objects
List Featured Products
GET /api/productos/destacados
Retrieve products marked as featured (destacado = true).
Authentication : None (public endpoint)
Response : 200 OK - Array of featured product objects
Use Case : Display featured products on the homepage or promotional sections.
Get Product by ID
Authentication : None (public endpoint)
Responses :
200 OK - Product object
404 Not Found - Product does not exist
Example Request :
curl -X GET http://localhost:8080/api/productos/1
Search Products by Name
GET /api/productos/buscar?nombre={query}
Search products by name (case-insensitive partial match).
Authentication : None (public endpoint)
Response : 200 OK - Array of matching products
Example Request :
curl -X GET "http://localhost:8080/api/productos/buscar?nombre=laptop"
List Products by Category
GET /api/productos/categoria/{id}
Retrieve all products in a specific category.
Authentication : None (public endpoint)
Response : 200 OK - Array of products in the category
Get Similar Products
GET /api/productos/{id}/similares
Retrieve products similar to the specified product (based on category).
Authentication : None (public endpoint)
Response : 200 OK - Array of similar products
Use Case : Display “You may also like” or “Related products” sections.
Create Product
Authentication : Required - ROLE_ADMIN
Request Body :
Category object with id field
Featured flag (default: false)
Response : 200 OK - Created product object with generated ID and slug
Example Request :
curl -X POST http://localhost:8080/api/productos \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"nombre": "Laptop Dell XPS 15",
"descripcion": "High-performance laptop with Intel i7",
"precio": 1299.99,
"stock": 10,
"categoria": {"id": 1},
"destacado": true
}'
The system automatically generates a URL-friendly slug from the product name (e.g., “laptop-dell-xps-15”).
Update Product
Authentication : Required - ROLE_ADMIN
Request Body : Same as Create Product (all fields)
Responses :
200 OK - Updated product object
404 Not Found - Product does not exist
Update Product Gallery
PUT /api/productos/{id}/galeria
Update the image gallery for a product. This endpoint replaces all existing images.
Authentication : Required - ROLE_ADMIN
Request Body : Array of image URL strings
Example Request :
curl -X PUT http://localhost:8080/api/productos/1/galeria \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '[
"https://example.com/images/product1-front.jpg",
"https://example.com/images/product1-back.jpg",
"https://example.com/images/product1-detail.jpg"
]'
Response : 200 OK
{
"message" : "Galería actualizada correctamente"
}
This operation uses orphan removal - any images not included in the new array will be permanently deleted from the database.
Soft Delete Product
DELETE /api/productos/{id}
Soft delete a product by setting activo = false. The product remains in the database but is hidden from public listings.
Authentication : Not specified (should be admin-only in production)
Response : 200 OK
{
"message" : "Estado del producto actualizado"
}
Permanently Delete Product
DELETE /api/productos/{id}/definitivo
Permanently remove a product from the database.
Authentication : Required - ROLE_ADMIN
Response : 204 No Content
This action is irreversible . All associated data (images, order details) will be affected. Use soft delete unless absolutely necessary.
Error Responses
Status Code Description 400 Bad RequestInvalid request body or parameters 401 UnauthorizedMissing or invalid JWT token 403 ForbiddenUser does not have ROLE_ADMIN 404 Not FoundProduct does not exist 500 Internal Server ErrorServer error
Integration Examples
Frontend: Display Product Catalog
async function loadProducts () {
const response = await fetch ( 'http://localhost:8080/api/productos/activos' );
const products = await response . json ();
products . forEach ( product => {
console . log ( ` ${ product . nombre } - $ ${ product . precio } ` );
console . log ( `Stock: ${ product . stock } ` );
});
}
Admin: Add New Product
async function createProduct ( productData , token ) {
const response = await fetch ( 'http://localhost:8080/api/productos' , {
method: 'POST' ,
headers: {
'Authorization' : `Bearer ${ token } ` ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ( productData )
});
return response . json ();
}
Categories API Manage product categories
Cart API Add products to shopping cart