Package com.example.springrest.dto
Record Class ProductRequest
java.lang.Object
java.lang.Record
com.example.springrest.dto.ProductRequest
public record ProductRequest(@NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") String name, @NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") BigDecimal price)
extends Record
Request DTO used to create or update a
Product.
Why a DTO? Keeps the HTTP contract stable and safe, decoupled from the JPA entity. Validation annotations ensure early, actionable feedback for clients.
Example (JSON):
{
"name": "Coffee Mug",
"price": 12.99
}
Usage (Controller):
@PostMapping("/products")
public ResponseEntity<ProductResponse> create(@Valid @RequestBody ProductRequest req) {
Product saved = productService.create(new Product(req.name(), req.price()));
return ResponseEntity
.created(URI.create("/products/" + saved.getId()))
.body(new ProductResponse(saved.getId(), saved.getName(), saved.getPrice()));
}
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionProductRequest(@NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") String name, @NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") BigDecimal price) Creates an instance of aProductRequestrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.@NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") Stringname()Returns the value of thenamerecord component.@NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") BigDecimalprice()Returns the value of thepricerecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
ProductRequest
public ProductRequest(@NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") @NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") String name, @NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") @NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") BigDecimal price) Creates an instance of aProductRequestrecord class.- Parameters:
name- the value for thenamerecord componentprice- the value for thepricerecord component
-
-
Method Details
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
name
@NotBlank(message="Name is mandatory") @Size(max=120, message="Name must be at most 120 characters") public @NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") String name()Returns the value of thenamerecord component.- Returns:
- the value of the
namerecord component
-
price
@NotNull(message="Price must be provided") @DecimalMin(value="0.01", inclusive=true, message="Price must be greater than 0") @Digits(integer=10, fraction=2, message="Price must have at most 2 decimal places") public @NotNull(message="Price must be provided") @DecimalMin(value="0.01",inclusive=true,message="Price must be greater than 0") @Digits(integer=10,fraction=2,message="Price must have at most 2 decimal places") BigDecimal price()Returns the value of thepricerecord component.- Returns:
- the value of the
pricerecord component
-