Recall & Review
beginner
What is the purpose of the @GetMapping annotation in Spring Boot?
The @GetMapping annotation is used to map HTTP GET requests to specific handler methods in a Spring Boot controller. It tells the app which method to run when a GET request is received at a certain URL.
Click to reveal answer
beginner
How do you specify the URL path for a GET request using @GetMapping?
You specify the URL path by passing it as a string value to @GetMapping, like @GetMapping("/hello"). This means the method handles GET requests sent to '/hello'.
Click to reveal answer
intermediate
Can @GetMapping handle query parameters? How?
Yes, @GetMapping can handle query parameters by using method parameters annotated with @RequestParam. For example, @GetMapping("/search") with a method parameter @RequestParam String keyword will capture '?keyword=value' from the URL.
Click to reveal answer
intermediate
What is the difference between @GetMapping and @RequestMapping(method = RequestMethod.GET)?
@GetMapping is a shortcut annotation specifically for GET requests. @RequestMapping(method = RequestMethod.GET) does the same but is more verbose. @GetMapping improves code readability.
Click to reveal answer
beginner
Show a simple example of a Spring Boot controller method using @GetMapping.
Example:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, world!";
}
}
This method returns 'Hello, world!' when a GET request is made to '/hello'.Click to reveal answer
What HTTP method does @GetMapping handle?
✗ Incorrect
@GetMapping is designed to handle HTTP GET requests only.
How do you specify the URL path in @GetMapping?
✗ Incorrect
The URL path is given as a string inside @GetMapping parentheses.
Which annotation is used to extract query parameters in a @GetMapping method?
✗ Incorrect
@RequestParam extracts query parameters from the URL.
What is the main advantage of @GetMapping over @RequestMapping(method = RequestMethod.GET)?
✗ Incorrect
@GetMapping is a shortcut annotation making code cleaner for GET requests.
What will the following method return when accessed via GET /hello?
@GetMapping("/hello")
public String greet() { return "Hi!"; }
✗ Incorrect
The method returns the string "Hi!" as the response body.
Explain how @GetMapping works in a Spring Boot controller and how to use it to handle a GET request.
Think about how the app knows which method to run for a GET URL.
You got /4 concepts.
Describe the difference between @GetMapping and @RequestMapping for GET requests and why you might prefer @GetMapping.
Focus on simplicity and clarity in code.
You got /4 concepts.