Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 22

    Validation

    Bean Validation (Jakarta Validation) declares constraints on DTO fields and Spring enforces them automatically when you add @Valid.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Bean Validation (Jakarta Validation) declares constraints on DTO fields and Spring enforces them automatically when you add @Valid. Invalid requests fail fast with a 400 — your service code stays clean.

    Informative example

    ts
    public record SignupRequest(
    @NotBlank @Email String email,
    @NotBlank @Size(min = 12, max = 72) String password,
    @NotBlank @Pattern(regexp = "[A-Za-z][A-Za-z '-]{1,49}") String name,
    @Min(13) @Max(120) int age
    ) {}
    @PostMapping("/signup")
    public UserDto signup(@Valid @RequestBody SignupRequest req) { ... }

    Best practices

    • Validate at the boundary, not deep inside services.
    • Custom rules → custom annotation + ConstraintValidator.
    • Group validation (@Validated(OnCreate.class)) when create vs update differ.
    Ready to mark this lesson complete?Track your journey across the entire course.