Bird
0
0

How can you combine @RequestBody with validation annotations to validate JSON input automatically?

hard📝 Application Q9 of 15
Spring Boot - REST Controllers
How can you combine @RequestBody with validation annotations to validate JSON input automatically?
AValidation is automatic without extra annotations
BUse @RequestParam with validation annotations
CAdd @Valid before @RequestBody parameter and use validation annotations in the model
DAdd @Validated on the controller class only
Step-by-Step Solution
Solution:
  1. Step 1: Understand validation with @RequestBody

    Adding @Valid before the @RequestBody parameter triggers validation of the JSON mapped object.
  2. Step 2: Use validation annotations in model

    Annotations like @NotNull in the User class define validation rules.
  3. Final Answer:

    Add @Valid before @RequestBody parameter and use validation annotations in the model -> Option C
  4. Quick Check:

    @Valid + @RequestBody triggers validation [OK]
Quick Trick: Use @Valid before @RequestBody to enable validation [OK]
Common Mistakes:
  • Using @RequestParam instead of @RequestBody for validation
  • Assuming validation works without @Valid
  • Only annotating controller class without @Valid on parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes