Overview
This endpoint creates a new product with the specified attributes and category associations. Categories are automatically created if they don’t already exist. The operation is transactional, ensuring data consistency.
Request Body
Detailed description of the product
Product price in the base currency (BigDecimal format, e.g., 29.99)
Initial stock quantity (must be a positive long integer)
Array of category names (strings). Categories will be created automatically if they don’t exist.
Response
The response includes the created product with generated fields and a Location header pointing to the new resource.
Auto-generated unique identifier for the product
Product name as provided in the request
Product description as provided in the request
List of category associations created for this product Product-category association ID
Category ID (auto-generated)
Timestamp when the product was created (auto-generated, ISO 8601 format)
Timestamp when the product was last updated (initially same as createdAt)
HATEOAS links for navigation
Status Codes
Product created successfully. The Location header contains the URI of the new product.
Invalid request body or missing required fields
Server error occurred while creating the product
Examples
cURL
cURL with New Category
JavaScript
Python
Java
curl -X POST "http://localhost:8080/products" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"name": "Wireless Mouse",
"description": "Ergonomic wireless mouse with adjustable DPI",
"price": 29.99,
"stockQuantity": 150,
"category": ["Electronics", "Computer Accessories"]
}'
Request Body Example
{
"name" : "Wireless Mouse" ,
"description" : "Ergonomic wireless mouse with adjustable DPI settings, 6 programmable buttons, and long battery life up to 18 months" ,
"price" : 29.99 ,
"stockQuantity" : 150 ,
"category" : [ "Electronics" , "Computer Accessories" ]
}
Success Response Example (201 Created)
{
"id" : 42 ,
"name" : "Wireless Mouse" ,
"description" : "Ergonomic wireless mouse with adjustable DPI settings, 6 programmable buttons, and long battery life up to 18 months" ,
"price" : 29.99 ,
"stockQuantity" : 150 ,
"categories" : [
{
"id" : 15 ,
"product" : null ,
"category" : {
"id" : 1 ,
"name" : "Electronics"
}
},
{
"id" : 16 ,
"product" : null ,
"category" : {
"id" : 3 ,
"name" : "Computer Accessories"
}
}
],
"createdAt" : "2026-03-03T10:30:00" ,
"updatedAt" : "2026-03-03T10:30:00" ,
"_links" : {
"self" : {
"href" : "http://localhost:8080/products/42"
},
"products" : {
"href" : "http://localhost:8080/products"
}
}
}
Response Headers:
Location: http://localhost:8080/products/42
Content-Type: application/json
Error Response Example (400 Bad Request)
{
"timestamp" : "2026-03-03T10:30:00" ,
"status" : 400 ,
"error" : "Bad Request" ,
"message" : "Validation failed" ,
"errors" : [
{
"field" : "price" ,
"message" : "must not be null"
}
],
"path" : "/products"
}
Transaction Management: This endpoint is annotated with @Transactional, which means if any part of the operation fails (product creation or category association), all changes will be rolled back to maintain database consistency.
Category Auto-Creation: If a category name in the category array doesn’t exist in the database, it will be automatically created. Existing categories are retrieved and reused by name.
All fields in the request body are required. Make sure to validate the data on the client side before sending the request to avoid 400 Bad Request errors.