0
0
Spring Bootframework~10 mins

@Valid annotation on request body in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Valid annotation on request body
Client sends HTTP request with JSON body
Spring Boot Controller receives request
@Valid triggers validation on request body object
Validation passes
Response sent back to client
When a client sends data, Spring Boot uses @Valid to check the data. If valid, the controller runs. If not, an error response is sent.
Execution Sample
Spring Boot
public ResponseEntity<String> createUser(@Valid @RequestBody User user) {
    return ResponseEntity.ok("User created");
}
This code checks the User object sent in the request body. If valid, it returns success.
Execution Table
StepActionInput DataValidation ResultController BehaviorResponse
1Receive HTTP POST with JSON body{"name":"Alice","age":25}Not yet validatedWaiting for validationNo response yet
2@Valid triggers validation on User object{"name":"Alice","age":25}Passes all constraintsController method executesNo response yet
3Controller returns success messageUser object validN/AReturns ResponseEntity.okHTTP 200 OK with 'User created'
4Receive HTTP POST with invalid JSON body{"name":"","age":-5}Not yet validatedWaiting for validationNo response yet
5@Valid triggers validation on User object{"name":"","age":-5}Fails: name blank, age negativeController method skippedSpring returns HTTP 400 Bad Request with error details
💡 Execution stops after sending HTTP response based on validation result
Variable Tracker
VariableStartAfter Step 2After Step 5Final
user.namenull"Alice"""""
user.agenull25-5-5
validationResultnullvalidinvalidinvalid
response.statusnull200 OK400 Bad Request400 Bad Request
Key Moments - 2 Insights
Why does the controller method not run when validation fails?
Because @Valid triggers validation before the method runs. If validation fails (see step 5 in execution_table), Spring stops and sends an error response instead.
What happens if the request body is missing required fields?
Validation fails since required constraints are not met. The execution_table row 5 shows validation failure and Spring returns 400 Bad Request.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result at step 2?
ANot yet validated
BFails due to missing fields
CPasses all constraints
DController skipped
💡 Hint
Check the 'Validation Result' column at step 2 in execution_table
At which step does Spring return a 400 Bad Request response?
AStep 5
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Response' column in execution_table for HTTP 400 Bad Request
If the user.age was 30 instead of -5 in step 5, what would change?
AValidation would still fail
BValidation would pass and controller runs
CResponse would be 400 Bad Request
DNo change in validation result
💡 Hint
Refer to variable_tracker for user.age values and validation results
Concept Snapshot
@Valid on @RequestBody triggers automatic validation of incoming data.
If data is valid, controller method runs normally.
If invalid, Spring returns 400 Bad Request with errors.
Use validation annotations on model fields (e.g., @NotBlank, @Min).
This helps catch bad input early and keeps code clean.
Full Transcript
When a client sends a request with data, Spring Boot uses the @Valid annotation on the request body to check if the data meets rules like not empty or minimum values. If the data is good, the controller method runs and sends a success response. If the data is bad, Spring stops the method and sends back an error message with status 400. This process helps keep the app safe and user-friendly by catching mistakes early.