0Pricing
Java Academy · Lesson

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

  1. Bean Validation: @NotNull, @Size, @Pattern
  2. Custom Constraint Annotations
  3. Global Exception Handling with @ControllerAdvice
  4. RFC 7807 Problem Details and Consistent Error Responses
← Back to Java Academy