Card 06/ 06
Recap: Validating Input
Constraints as annotations, @Valid as the switch, and errors a client can act on.
Validating input comes down to the four ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
Constraints such as @NotBlank and @Min sit as annotations on the fields they apply to | Bean Validation: Constraints Written as Annotations |
@Valid on the parameter makes Spring check them and answer 400 | Bean Validation Example: @Valid Rejects a Bad Request |
Without @Valid, the same annotations check nothing | Missing @Valid: Constraint Annotations That Check Nothing |
Handling MethodArgumentNotValidException turns a bare 400 into field-by-field errors | Turning Validation Errors Into a Field-by-Field Response |
In practice all four go together: annotate the request class, put @Valid on the parameter, and handle the exception once for every controller.
← → move through sections and cards