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

    Constructors
    Constructor
    Description
    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)
    Creates an instance of a ProductRequest record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    @NotBlank(message="Name is mandatory") @Size(max=120,message="Name must be at most 120 characters") String
    Returns the value of the name record 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") BigDecimal
    Returns the value of the price record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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 a ProductRequest record class.
      Parameters:
      name - the value for the name record component
      price - the value for the price record component
  • Method Details

    • toString

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • 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 the name record component.
      Returns:
      the value of the name record 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 the price record component.
      Returns:
      the value of the price record component