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

    Validation

    Bean Validation (Jakarta) checks Orders request DTOs before business logic runs.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Bean Validation (Jakarta) checks Orders request DTOs before business logic runs. Add spring-boot-starter-validation, annotate records with @NotBlank, @Min, @Positive, and put @Valid on @RequestBody parameters.

    On Boot 3 the package is jakarta.validation. Without the starter, annotations are silently ignored — a dangerous footgun.

    Controller validation catches malformed input. Domain rules (inventory available, SKU sellable) still belong in the service and may throw conflicts even when the DTO is valid.

    @Validated on a class enables method validation for parameters like @Max on query params.

    Purpose of this lesson

    Enable Jakarta validation for Orders request bodies and understand starter + @Valid requirements.

    Understanding the topic

    Validation annotations are declarative — wiring makes them real.

    • starter-validation: required dependency.
    • @Valid / @Validated: triggers the validator at the boundary.
    • Constraints: NotBlank, NotNull, Min, Max, Email, Pattern, Positive.
    • Errors: MethodArgumentNotValidException → advice → 400.
    • Groups: advanced; skip until you have multiple validation contexts.

    Step-by-step explanation

    1. Add the validation starter if missing.
    2. Annotate CreateOrderRequest.
    3. Put @Valid on the controller parameter.
    4. Post invalid JSON and confirm 400 via advice.

    Informative example

    Request constraints.

    java
    public record CreateOrderRequest(
    @NotBlank String sku,
    @Min(1) @Max(99) int qty
    ) {}
    @PostMapping
    OrderView create(@Valid @RequestBody CreateOrderRequest body) {
    return orders.place(body);
    }

    Real-world use

    Bots will post qty=0 and empty SKUs. Validation stops those at the edge so OrderService does not create junk rows or burn pricing calls.

    Security implications

    Validation is a first line of defense, not the only one.

    • Length-limit strings to protect DB columns and logs.
    • Reject unexpected nested payloads.
    • Do not trust validated input for authorization decisions.

    Best practices

    • Validate at the boundary and re-check critical invariants in the domain.
    • Return field-level errors in a consistent problem/validation payload.
    • Keep messages safe and stable for clients.

    Common mistakes

    • Forgetting the validation starter — annotations do nothing.
    • Using javax.validation imports on Boot 3.
    • Relying only on UI validation.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat package do Boot 3 validation annotations use?+

    Answer

    jakarta.validation.
    2IntermediateQuestionWhy might @NotBlank appear to do nothing?+

    Answer

    Missing spring-boot-starter-validation or missing @Valid/@Validated at the call site.
    3AdvancedQuestionBoundary validation vs domain rules?+

    Answer

    Boundary checks shape/requiredness; domain checks business invariants that need services or data.

    Hands-on exercise

    Lab. Post {"sku":"","qty":0}. Confirm 400. Remove @Valid temporarily and observe the bad data reaching the service — then put it back.

    Summary

    Orders rejects bad input early with Jakarta validation. Next you log what happened without leaking secrets.

    Ready to mark this lesson complete?Track your journey across the entire course.