Validate a List of dto (validation annotations put on the dto aren't working)

Viewed 31

I have a DTO class with validation annotations & in a Post API request, I have to take a List of this DTO, but the validations that I have added in the DTO aren't working.

@PostMapping("/test")
public MyTinyDto test(@Valid @RequestBody List<MyTinyDto> myDtos) {
    return myDtos.get(0);
}


@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyTinyDto {
    @Min(value = 10,
            message = "Min value of Integer is ten")
    Integer x;
}

Postman Request

1 Answers

Add @Validated to class rest controller.

@RestController
@RequestMapping("/api")
@Validated
public class Test{

    @PostMapping("/test")
    public MyTinyDto test(@Valid @RequestBody List<MyTinyDto> myDtos) {
        return myDtos.get(0);

}
Related