Package com.example.springrest.dto


package com.example.springrest.dto
Data Transfer Objects (DTOs) for the Product API.

Why use DTOs instead of exposing entities directly?

  • Separation of concerns: Entities map to database tables, while DTOs define the shape of data exposed in the API.
  • Security: Entities may contain internal fields (e.g., audit data, relationships) that should not be visible to clients.
  • Flexibility: DTOs allow us to evolve the API independently of the database schema. For example, we can rename fields or combine data from multiple entities.
  • Validation: Request DTOs (like ProductRequest) carry validation rules to ensure only valid data reaches the service layer.
  • Clarity in API docs: Response DTOs (like ProductResponse) produce clean Swagger/OpenAPI documentation with examples.

In short: Entities are for persistence; DTOs are for communication.

  • Record Classes
    Class
    Description
    Request DTO used to create or update a Product.
    Response DTO returned to clients for Product resources.