@RestController. What will be the HTTP response body when accessing /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 write the return value of the method directly to the HTTP response body. Since the method returns a String, the response body will be exactly that string.
/submit and returns a confirmation string.Option B correctly uses @PostMapping("/submit") with the leading slash and @RestController. Option B uses GET instead of POST. Option B uses @Controller without @ResponseBody, so it won't return the string as response body. Option B misses the leading slash in the path, which is required.
/api/data return 404 Not Found?import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DataController { @GetMapping("api/data") public String getData() { return "Data"; } }
Spring expects the path in mapping annotations to start with a leading slash. Without it, the mapping does not match the requested URL, causing 404.
@Controller and @RestController in Spring Boot.@RestController is a shortcut for @Controller plus @ResponseBody. It means the return value of methods is written directly to the HTTP response body, usually as JSON or plain text. @Controller is used for MVC views and requires @ResponseBody to return data directly.
/error is accessed?import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ErrorController { @GetMapping("/error") public String error() { throw new IllegalStateException("Something went wrong"); } }
By default, Spring Boot returns a JSON error response with HTTP status 500 when an exception is thrown in a @RestController method. The JSON includes error details like timestamp, status, error, and message.