Bird
Raised Fist0
Spring Bootframework~20 mins

@GetMapping for GET requests in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Boot @GetMapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?
Consider this Spring Boot controller method annotated with @GetMapping. What will be the HTTP response body when a GET request is made to /hello?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}
AA 404 Not Found error because no view is returned
B"Hello, World!" as plain text in the HTTP response body
CA JSON object {"message": "Hello, World!"}
DAn empty HTTP response with status 200 OK
Attempts:
2 left
💡 Hint
Remember that @RestController combines @Controller and @ResponseBody, so the return value is sent directly as the response body.
📝 Syntax
intermediate
1:30remaining
Which @GetMapping annotation syntax correctly maps to the URL path "/users/{id}"?
You want to create a GET endpoint that accepts a user ID as a path variable. Which of the following @GetMapping annotations is correct?
A@GetMapping("/users/{id}")
B@GetMapping("/users/${id}")
C@GetMapping("/users/:id")
D@GetMapping("/users/id")
Attempts:
2 left
💡 Hint
Path variables in Spring Boot use curly braces {} in the URL pattern.
🔧 Debug
advanced
2:30remaining
Why does this @GetMapping method cause a 405 Method Not Allowed error?
Examine the following controller code. When a GET request is sent to "/data", the server responds with 405 Method Not Allowed. Why?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController {
    @PostMapping("/data")
    public String postData() {
        return "Data posted";
    }

    @GetMapping("/data")
    public String getData() {
        return "Data fetched";
    }
}
AThere is a conflict because both @PostMapping and @GetMapping use the same path, causing ambiguity
BThe @GetMapping method is missing @ResponseBody, so it does not handle GET requests properly
CThe controller class is annotated with @RestController, so both methods should work; the error is elsewhere
DThe server is misconfigured and blocks GET requests to /data
Attempts:
2 left
💡 Hint
Check if the annotations and method signatures are correct and if the server supports GET requests.
❓ state_output
advanced
2:00remaining
What is the value of the path variable 'id' when this GET request is handled?
Given this controller method, what will be the value of the parameter 'id' when a GET request is made to /items/42?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ItemController {
    @GetMapping("/items/{id}")
    public String getItem(@PathVariable String id) {
        return "Item ID: " + id;
    }
}
Anull because the parameter type is String but the path variable is numeric
B42 as an Integer
CAn error because @PathVariable requires an int parameter for numeric values
D"42" as a String
Attempts:
2 left
💡 Hint
Path variables are converted to the parameter type if possible. String works for any value.
🧠 Conceptual
expert
2:00remaining
Which statement about @GetMapping in Spring Boot is TRUE?
Select the one true statement about the @GetMapping annotation in Spring Boot.
A@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)
B@GetMapping requires the method to have no parameters
C@GetMapping automatically serializes the return value to JSON regardless of controller type
D@GetMapping can only be used on methods that return a view name, not on REST endpoints
Attempts:
2 left
💡 Hint
Think about what @GetMapping replaces or simplifies in Spring MVC.

Practice

(1/5)
1. What is the main purpose of the @GetMapping annotation in a Spring Boot application?
easy
A. To map HTTP POST requests to specific controller methods
B. To map HTTP GET requests to specific controller methods
C. To configure database connections
D. To handle exceptions globally

Solution

  1. Step 1: Understand HTTP methods in Spring Boot

    Spring Boot uses annotations to map HTTP methods like GET, POST, etc., to controller methods.
  2. Step 2: Identify the role of @GetMapping

    @GetMapping specifically maps HTTP GET requests to the annotated method, so when a user visits a URL with a GET request, this method runs.
  3. Final Answer:

    To map HTTP GET requests to specific controller methods -> Option B
  4. Quick Check:

    @GetMapping = maps GET requests [OK]
Hint: Remember: @GetMapping is for GET requests only [OK]
Common Mistakes:
  • Confusing @GetMapping with @PostMapping
  • Thinking @GetMapping handles POST or PUT requests
  • Assuming it configures database or exceptions
2. Which of the following is the correct syntax to map a GET request to the URL /hello using @GetMapping in Spring Boot?
easy
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"; }

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]
Hint: 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
3. Given the following Spring Boot controller method, what will be the output when a GET request is made to /welcome?
@GetMapping("/welcome")
public String welcome() {
    return "Welcome to Spring Boot!";
}
medium
A. An empty response
B. HTTP 404 Not Found error
C. "Welcome to Spring Boot!"
D. HTTP 500 Internal Server Error

Solution

  1. Step 1: Understand the method mapping and return value

    The method is mapped to GET requests at "/welcome" and returns a plain string.
  2. Step 2: Determine the response content

    Since the method returns a string, Spring Boot will send "Welcome to Spring Boot!" as the response body for the GET request.
  3. Final Answer:

    "Welcome to Spring Boot!" -> Option C
  4. Quick Check:

    GET /welcome returns method's string [OK]
Hint: Returned string is sent as response body by default [OK]
Common Mistakes:
  • Expecting a view name instead of response text
  • Confusing 404 error when mapping exists
  • Assuming empty or error response without cause
4. Identify the error in the following Spring Boot controller method:
@GetMapping("/user/{id}")
public String getUser(@PathVariable int userId) {
    return "User ID: " + userId;
}
medium
A. The @PathVariable parameter name does not match the path variable name
B. The method should return a ResponseEntity instead of String
C. The @GetMapping path should not contain curly braces
D. The method parameter should be of type String, not int

Solution

  1. Step 1: Compare path variable name and method parameter

    The path is defined as /user/{id} but the method parameter is named userId without specifying the variable name in @PathVariable.
  2. Step 2: Understand @PathVariable name matching

    By default, the parameter name must match the path variable name or be explicitly specified like @PathVariable("id") int userId.
  3. Final Answer:

    The @PathVariable parameter name does not match the path variable name -> Option A
  4. Quick Check:

    Path variable names must match or be explicitly linked [OK]
Hint: Match @PathVariable name with URL placeholder or specify explicitly [OK]
Common Mistakes:
  • Not matching method parameter name with path variable
  • Assuming type mismatch causes error here
  • Thinking curly braces are invalid in @GetMapping path
5. You want to create a Spring Boot controller method that handles GET requests to /product/{category}/{id} and returns a string combining both path variables. Which of the following method signatures correctly uses @GetMapping and @PathVariable annotations?
hard
A. @GetMapping("/product/{category}/{id}") public String getProduct(String category, int id) { return category + "-" + id; }
B. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable String cat, @PathVariable int id) { return cat + "-" + id; }
C. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable int category, @PathVariable String id) { return category + "-" + id; }
D. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; }

Solution

  1. Step 1: Check @GetMapping path and method parameters

    The path has two variables: {category} and {id}. The method must accept two parameters annotated with @PathVariable matching these names or explicitly linked.
  2. Step 2: Analyze each option's parameter annotations and types

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable String cat, @PathVariable int id) { return cat + "-" + id; } uses parameter names that do not match path variables (cat != category), so fails without explicit names. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } explicitly links parameter names to path variables, which is safest. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable int category, @PathVariable String id) { return category + "-" + id; } swaps types incorrectly. @GetMapping("/product/{category}/{id}") public String getProduct(String category, int id) { return category + "-" + id; } lacks @PathVariable annotations, so variables won't be bound.
  3. Step 3: Choose the best correct and safe option

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } explicitly names variables, avoiding errors if parameter names differ from path variables.
  4. Final Answer:

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } -> Option D
  5. Quick Check:

    Explicit @PathVariable names ensure correct binding [OK]
Hint: Use @PathVariable("name") if parameter name differs from path variable [OK]
Common Mistakes:
  • Omitting @PathVariable annotation
  • Mismatching parameter types with path variables
  • Not explicitly naming @PathVariable when names differ
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard