Bird
0
0

What is wrong with this method declaration?

medium📝 Debug Q7 of 15
Spring Boot - REST Controllers
What is wrong with this method declaration?
@GetMapping("/data")
public String getData(@RequestParam(required = false) int count) {
    return "Count: " + count;
}
AMethod should use @RequestBody instead
BPrimitive int cannot be null, so optional param causes error
Crequired=false is invalid for @RequestParam
DMissing defaultValue causes compilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand primitive type behavior

    Primitive int cannot be null, so if the parameter is missing, Spring cannot assign null.
  2. Step 2: Check optional param with primitive

    Using required=false without defaultValue on primitive causes error because no value can be assigned.
  3. Final Answer:

    Primitive int cannot be null, so optional param causes error -> Option B
  4. Quick Check:

    Use wrapper types for optional primitives [OK]
Quick Trick: Use Integer wrapper for optional int params [OK]
Common Mistakes:
  • Using primitive types for optional params
  • Expecting default null for primitives
  • Confusing required=false with defaultValue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes