Validation
Валидация входных данных в Spring простыми словами: где валидировать (границы vs домен), Bean Validation (@Valid, @NotNull, @Size), свои ограничения, межполевые проверки и сообщения об ошибках.
A compact section on input validation in Spring: where to check data, how to use Bean Validation, and when to write custom constraints. It complements the breakdown of REST errors and the validation style guide.
- Where to validate data — Boundary or domain — we figure out where to put @Valid, where to keep business invariants, and why duplicating checks across all layers is harmful.
- Bean Validation in Spring: @NotNull, @Valid and MethodArgumentNotValidException — How Jakarta Bean Validation works in Spring Boot: the @NotNull, @Size, @Email annotations on DTO fields, @Valid in the controller, cascading for nested objects, and what happens on an error.
- Custom constraints and error messages — How to write a custom constraint via @Constraint and ConstraintValidator, check several fields at once, and configure error texts through messages.properties.