Validation
Bean Validation (Jakarta) checks Orders request DTOs before business logic runs.
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
- Add the validation starter if missing.
- Annotate
CreateOrderRequest. - Put
@Validon the controller parameter. - Post invalid JSON and confirm 400 via advice.
Informative example
Request constraints.
public record CreateOrderRequest(@NotBlank String sku,@Min(1) @Max(99) int qty) {}@PostMappingOrderView 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.validationimports on Boot 3. - Relying only on UI validation.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat package do Boot 3 validation annotations use?+
Answer
jakarta.validation.2IntermediateQuestionWhy might @NotBlank appear to do nothing?+
Answer
spring-boot-starter-validation or missing @Valid/@Validated at the call site.3AdvancedQuestionBoundary validation vs domain rules?+
Answer
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.