0
0
Spring Bootframework~5 mins

@GetMapping for GET requests in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AGET
BPOST
CPUT
DDELETE
How do you specify the URL path in @GetMapping?
AAs a method parameter
BAs a string value inside parentheses, e.g., @GetMapping("/path")
CUsing @RequestParam
DUsing @PathVariable
Which annotation is used to extract query parameters in a @GetMapping method?
A@PathVariable
B@RequestBody
C@RequestParam
D@ResponseBody
What is the main advantage of @GetMapping over @RequestMapping(method = RequestMethod.GET)?
AIt is shorter and clearer for GET requests
BIt supports POST requests
CIt automatically handles query parameters
DIt requires no URL path
What will the following method return when accessed via GET /hello? @GetMapping("/hello") public String greet() { return "Hi!"; }
AAn error
BEmpty response
Cnull
D"Hi!"
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.