How does Spring Boot support data validation?

Spring Boot supports data validation by integrating the Java Bean Validation API (JSR-380), implemented by Hibernate Validator, directly into the web layer. Developers annotate model fields with constraint annotations and Spring automatically validates incoming request data before it reaches business logic.

Key Points: • Constraint annotations like @NotNull, @Size, @Email, and @Min are placed on model or DTO fields. • @Valid or @Validated on a controller method parameter triggers validation before the handler runs. • Validation failures throw a MethodArgumentNotValidException, typically translated into a 400 response. • Custom constraints can be built by implementing ConstraintValidator for domain-specific rules. • Validation groups allow different rule sets to apply for create versus update operations.

Example: A UserDto class can declare @NotBlank on a name field and @Email on an email field; when a controller method is annotated with @Valid, Spring rejects malformed requests automatically without extra manual checks in the handler.

Code Example:

public class UserDto {
    @NotBlank
    private String name;

    @Email
    private String email;
}

@PostMapping("/users")
public ResponseEntity<User> create(@Valid @RequestBody UserDto dto) {
    return ResponseEntity.ok(userService.create(dto));
}

Interview Tip: A concise interview answer is:

"Spring Boot validates data using the JSR-380 Bean Validation API via Hibernate Validator. I annotate DTO fields with constraints like @NotNull or @Size, then add @Valid on the controller parameter, and Spring rejects invalid requests with a 400 before my business logic even runs."