@Controller & @RestController
Introduction
In Spring Boot, a controller is a component that handles HTTP requests and produces responses. Spring provides two main annotations: @Controller and @RestController.
@Controller
@Controller is used on a class responsible for handling requests. It's usually combined with @ResponseBody on methods to return data directly (rather than a view name).
@Controller
@RequestMapping("/students")
public class StudentController {
@ResponseBody
@GetMapping("/{id}")
public Student getById(@PathVariable Long id) {
return studentService.findById(id);
}
}
Without @ResponseBody, the return value is interpreted as a view (template) name.
@RestController
@RestController is a shorthand for @Controller + @ResponseBody at the class level. All methods automatically return data (JSON/XML), not a view name.
@RestController
@RequestMapping("/api/students")
public class StudentRestController {
@GetMapping
public List<Student> getAll() {
return studentService.findAll();
}
@PostMapping
public Student create(@RequestBody Student student) {
return studentService.save(student);
}
}
Key Differences
| Feature | @Controller | @RestController |
|---|---|---|
| Default return type | View name (String) | Response body (Object/JSON) |
| Requires @ResponseBody | Yes, per method | No, automatic |
| Best for | MVC + Thymeleaf | REST API |
| Content type | HTML | JSON / XML |
@ResponseStatus
This annotation sets the HTTP status code that is returned.
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Student create(@RequestBody Student student) {
return studentService.save(student);
}
Return Type: ResponseEntity
ResponseEntity gives you full control over the status code, headers, and response body.
@GetMapping("/{id}")
public ResponseEntity<Student> getById(@PathVariable Long id) {
return studentService.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
| Return Type | Use Case |
|---|---|
ResponseEntity<T> |
Full control (status, headers, body) |
T (Object) |
Spring automatically serializes to JSON |
String |
Plain text response |
void |
Status 200 only, no body |
Simple CRUD Example
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
public List<User> index() {
return userService.getAll();
}
@GetMapping("/{id}")
public User show(@PathVariable Long id) {
return userService.getById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User store(@RequestBody User user) {
return userService.save(user);
}
@PutMapping("/{id}")
public User update(@PathVariable Long id, @RequestBody User user) {
return userService.update(id, user);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void destroy(@PathVariable Long id) {
userService.delete(id);
}
}
Summary
- Use
@RestControllerfor REST API (JSON). - Use
@Controllerfor MVC (HTML/templates). ResponseEntityis a good fit when you need flexible responses.
Next chapter: We'll take a closer look at Request Mapping — how to define the path, method, and parameters of an endpoint.