0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.