What is @RestController in Spring Boot?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Spring Boot basics — a prerequisite for any developer role.

Answer

@RestController is a convenience annotation that combines @Controller and @ResponseBody. It marks a class as a REST API controller where every method automatically serializes its return value to the HTTP response body (typically JSON) without needing @ResponseBody on each method. @Controller alone is used for MVC controllers that return view names (HTML templates). Example: @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getAllUsers() { return userService.findAll(); } @GetMapping("/{id}") public ResponseEntity<User> getUserById(@PathVariable Long id) { return userService.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build()); } @PostMapping public ResponseEntity<User> createUser(@Valid @RequestBody CreateUserRequest request) { User user = userService.create(request); return ResponseEntity.status(HttpStatus.CREATED).body(user); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody UpdateUserRequest request) { return userService.update(id, request); } @DeleteMapping("/{id}") public ResponseEntity<Void> deleteUser(@PathVariable Long id) { userService.delete(id); return ResponseEntity.noContent().build(); } }. Jackson automatically serializes Java objects to JSON and deserializes JSON request bodies.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.