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"; } }
In Spring Boot, a class annotated with @RestController combines @Controller and @ResponseBody. This means the returned String is sent as the HTTP response body directly.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DataController { @PostMapping("/submit") public void submitData() { // process data } }
When a controller method returns void and completes without errors, Spring Boot sends a 200 OK status by default with an empty response body.
The @PathVariable annotation binds the method parameter to the URI template variable. Option A correctly uses it with the matching path.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ApiController { @GetMapping("/api/data/") public String getData() { return "Data"; } }
Spring Boot treats '/api/data' and '/api/data/' as different URLs. The trailing slash in the mapping means '/api/data' does not match, causing 404.
Understanding the request flow helps developers place filters and interceptors efficiently, avoiding extra work and improving response times.
