Review Create Dto Mapper

In our restaurant review platform, we need to safely transfer review data from our API to our service layer.

Data Transfer Objects

Let's implement the ReviewCreateUpdateRequestDto class that will handle incoming review creation requests:

package com.devtiro.restaurant.domain.dtos; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class ReviewCreateUpdateRequestDto { @NotBlank(message = "Review content is required") private String content; @NotNull(message = "Rating is required") @Min(value = 1, message = "Rating must be between 1 and 5") @Max(value = 5, message = "Rating must be between 1 and 5") private Integer rating; private List<String> photoIds = new ArrayList<>(); }

The validation annotations in our DTO ensure data quality:

  • @NotBlank checks that the content field is not empty or just whitespace
  • @NotNull ensures the rating is provided
  • @Min and @Max enforce our rating scale from 1 to 5

Mapper Implementation

We need to convert our DTO to our domain model using a mapper.

Let's create the ReviewMapper interface, adding the toCreateReviewRequest method:

package com.devtiro.restaurant.mappers; import com.devtiro.restaurant.domain.ReviewCreateUpdateRequest; import com.devtiro.restaurant.domain.dtos.ReviewCreateUpdateRequestDto; import com.devtiro.restaurant.domain.dtos.ReviewDto; import com.devtiro.restaurant.domain.entities.Review; import org.mapstruct.Mapper; import org.mapstruct.ReportingPolicy; @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface ReviewMapper { ReviewCreateUpdateRequest toReviewCreateUpdateRequest(ReviewCreateUpdateRequestDto dto); ReviewDto toDto(Review review); }

Summary

  • Created ReviewCreateUpdateRequestDto to validate incoming review data
  • Added validation annotations to ensure data quality
  • Implemented ReviewMapper interface with a toReviewCreateUpdateRequest and toDto methods
© 2026 Devtiro Ltd. All rights reserved