Performance: @RestController annotation
This affects server response time and client perceived load speed by simplifying REST API endpoint creation.
@RestController public class MyController { @RequestMapping("/data") public String getData() { return "Hello"; } }
@Controller public class MyController { @RequestMapping("/data") @ResponseBody public String getData() { return "Hello"; } }
| Pattern | Server Processing | Annotation Overhead | Response Consistency | Verdict |
|---|---|---|---|---|
| @Controller + @ResponseBody | Moderate | Higher due to multiple annotations | Consistent if used correctly | [!] OK |
| @RestController | Lower | Minimal, combined annotation | Always consistent JSON/text response | [OK] Good |
@RestController annotation in Spring Boot?/hello endpoint?
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring!";
}
}@RestController
public class UserController {
@GetMapping("/user")
public User getUser() {
User user = new User();
user.setName("Alice");
return user;
}
}
Assume User is a simple POJO with a name field and getters/setters.@RestController to achieve this?Pass the test and this pattern moves unknown