/hello?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!"; } }
The @RestController annotation tells Spring Boot to send the return value of the method directly as the HTTP response body. Since the method returns a String, the response body will contain exactly that string as plain text.
Option B is incorrect because the method returns a String, not a JSON object. Option B is wrong because the method handles the GET request properly. Option B is wrong because the response body is not empty.
In Spring Boot, path variables are defined using curly braces in the URL pattern, like /users/{id}. This tells Spring to capture the value in that part of the URL and pass it to the method.
Options A, C, and D use incorrect syntax for path variables.
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"; } }
Both methods are correctly annotated and the controller uses @RestController, which combines @Controller and @ResponseBody. Having both @PostMapping and @GetMapping on the same path is allowed and not ambiguous.
A 405 error usually means the HTTP method is not allowed by the server or a filter. The code itself is correct, so the problem is likely outside this snippet, such as server configuration or security settings blocking GET requests.
/items/42?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; } }
The path variable 'id' is captured as a String because the method parameter is String. The value "42" from the URL is passed as the string "42".
Spring Boot can convert path variables to many types, but String works for all values without error.
@GetMapping is a shortcut annotation that is equivalent to using @RequestMapping with method = RequestMethod.GET. It can be used on any method handling GET requests, whether returning views or REST data.
Option A is false because @GetMapping can be used for REST endpoints. Option A is false because serialization depends on @RestController or @ResponseBody, not @GetMapping itself. Option A is false because methods can have parameters, such as path variables or request parameters.