Bean Validation: @NotNull, @Size, @Pattern
Annotate DTOs with Bean Validation constraints and trigger validation with @Valid in controllers.
What Is Bean Validation?
Bean Validation (Jakarta Bean Validation 3.0) provides declarative constraint annotations for Java beans. Spring Boot auto-configures a validator and triggers it via @Valid or @Validated.
@NotNull, @NotEmpty, @NotBlank
@NotNull: value must not be null. @NotEmpty: not null and not empty (strings/collections). @NotBlank: not null, not empty, not whitespace-only. Prefer @NotBlank for strings.
public record CreateUserRequest(
@NotBlank(message = "Name is required")
String name,
@NotBlank @Email
String email,
@NotNull
UserRole role
) {}All lessons in this course
- Bean Validation: @NotNull, @Size, @Pattern
- Custom Constraint Annotations
- Global Exception Handling with @ControllerAdvice
- RFC 7807 Problem Details and Consistent Error Responses