In short
Never trust what the client sends. A blank name, an email without @ or a negative quantity should be stopped at the door, with a clear message, before it reaches your database.
Jakarta Bean Validation annotations like @NotBlank, @Email, @Size and @Min on request DTOs, plus @Valid on the controller parameter, reject bad input before it reaches your business logic.

Think of it like this
The security check at the mall
At the mall entrance in Ahmedabad, the guard checks every bag before you go in. Something not allowed? You're stopped right there with a reason, and the shops inside never deal with it. Validation annotations are the checklist, @Valid is the guard, and a 400 response is the polite "not allowed, here's why".
Watch it happen
A bad sign-up request
1/4Client sends {"name": "", "email": "ravi.gmail.com", "age": 15}.
The big ideas
What you need to know
Add the starter
spring-boot-starter-validation brings Jakarta Bean Validation with Hibernate Validator.
Common annotations
@NotNull, @NotBlank, @Size(min, max), @Email, @Min, @Max, @Positive, @Pattern.
@Valid triggers it
Put @Valid before @RequestBody. Without it, annotations on the DTO are ignored.
Custom messages
Each annotation takes message = "..." so the client sees something human-friendly.
See it in code
Walk through the code
1import jakarta.validation.Valid;2import jakarta.validation.constraints.*;3import org.springframework.http.HttpStatus;4import org.springframework.web.bind.annotation.*;5 6record SignupRequest(7 @NotBlank(message = "Name is required")8 @Size(max = 50, message = "Name must be at most 50 characters")9 String name,10 11 @NotBlank @Email(message = "Enter a valid email")12 String email,13 14 @Pattern(regexp = "[6-9]\\d{9}", message = "Enter a 10-digit Indian mobile number")15 String phone,16 17 @Min(value = 18, message = "You must be at least 18")18 int age19) {}20 21@RestController22@RequestMapping("/api/users")23class UserController {24 @PostMapping25 @ResponseStatus(HttpStatus.CREATED)26 public String signup(@Valid @RequestBody SignupRequest req) {27 return "Welcome, " + req.name();28 }29}1/4Two rules on name: not blank and at most 50 characters.
Annotations go on the record components. The \\d in Java source becomes the regex \d for a digit.
Save yourself the pain
Common mistakes
Oops
Adding annotations to the DTO but forgetting @Valid in the controller.
Do this instead
Without @Valid nothing is checked. Put it right before @RequestBody.
Oops
Using @NotNull for text fields.
Do this instead
@NotNull still allows "" and " ". Use @NotBlank for strings that must have content.
Oops
Relying only on frontend validation.
Do this instead
Anyone can call your API directly with curl. Always validate on the server too.
Your mission
Validate a sign-up form
Quick check
Which annotation rejects null, empty and whitespace-only strings?
Go deeper
- 1Add validation annotations to a CreateTaskRequest DTO.
- 2Send invalid JSON and inspect the 400 response.
- 3Write a custom validator for a rule such as a valid Indian PIN code.
Also part of these roadmaps
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.