Bird
0
0

A developer wants to validate a query parameter in a Spring Boot controller method. Which is the correct way to do this?

medium📝 Debug Q7 of 15
Spring Boot - Request and Response Handling
A developer wants to validate a query parameter in a Spring Boot controller method. Which is the correct way to do this?
AAdd <code>@NotNull</code> on the controller class.
BAdd <code>@RequestBody</code> on the query parameter.
CAdd <code>@NotNull</code> on the query parameter and annotate the controller with <code>@Validated</code>.
DAdd <code>@Valid</code> on the controller class only.
Step-by-Step Solution
Solution:
  1. Step 1: Understand validation on query parameters

    Query parameters are simple method parameters and require @Validated on the controller to activate validation.
  2. Step 2: Use @NotNull on the parameter to trigger validation

    Adding @NotNull on the query parameter triggers validation of constraints like @NotNull.
  3. Final Answer:

    Add @NotNull on the query parameter and annotate the controller with @Validated. -> Option C
  4. Quick Check:

    Query param validation needs @Validated + @NotNull = C [OK]
Quick Trick: Use @Validated on controller and @Valid on query params [OK]
Common Mistakes:
  • Using @RequestBody on query parameters
  • Placing constraints on controller class only
  • Omitting @Validated on controller

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes