Card 06/ 06

RecapDifficulty: Advanced1 min

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.

What the topic established, and which card it was on
The ideaWhere
Constraints such as @NotBlank and @Min sit as annotations on the fields they apply toBean Validation: Constraints Written as Annotations
@Valid on the parameter makes Spring check them and answer 400Bean Validation Example: @Valid Rejects a Bad Request
Without @Valid, the same annotations check nothingMissing @Valid: Constraint Annotations That Check Nothing
Handling MethodArgumentNotValidException turns a bare 400 into field-by-field errorsTurning 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.