Bird
0
0

Which of the following is the correct syntax to map a GET request to the URL /hello using @GetMapping in Spring Boot?

easy📝 Syntax Q12 of 15
Spring Boot - REST Controllers
Which of the following is the correct syntax to map a GET request to the URL /hello using @GetMapping in Spring Boot?
A@GetMapping(path = "/hello") public String greet() { return "Hi"; }
B@GetMapping(/hello) public String greet() { return "Hi"; }
C@GetMapping('/hello') public String greet() { return "Hi"; }
D@GetMapping(path = hello) public String greet() { return "Hi"; }
Step-by-Step Solution
Solution:
  1. Step 1: Check the correct use of @GetMapping syntax

    @GetMapping requires the path to be a string, usually passed as path = "/url" or simply "/url".
  2. Step 2: Identify correct string usage and syntax

    @GetMapping(path = "/hello") public String greet() { return "Hi"; } uses @GetMapping(path = "/hello") which is correct. @GetMapping(/hello) public String greet() { return "Hi"; } misses quotes around /hello. @GetMapping('/hello') public String greet() { return "Hi"; } uses single quotes, invalid for Java string literal. @GetMapping(path = hello) public String greet() { return "Hi"; } misses quotes around hello.
  3. Final Answer:

    @GetMapping(path = "/hello") public String greet() { return "Hi"; } -> Option A
  4. Quick Check:

    Correct syntax needs quotes and path attribute [OK]
Quick Trick: Always use quotes around URL paths in @GetMapping [OK]
Common Mistakes:
  • Omitting quotes around the path string
  • Forgetting the leading slash in URL
  • Using path without quotes causing syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes