Spring MVC vs Spring REST — View-Based vs API-Based
Fast recall
Quick revision
Core answer
Understanding that Spring REST is built on Spring MVC architecture, not a separate framework, and knowing when to use each approach
Thinking Spring REST is a different framework or module — it's just Spring MVC with @ResponseBody on every method, returning data instead of views
Explain that @RestController is syntactic sugar for @Controller plus @ResponseBody, and both use the same DispatcherServlet — the only difference is response handling (view rendering vs JSON serialization)
Interview-ready guide
Interview answer
A @Controller method returning "orders" can render templates/orders.html through a ViewResolver. Another @Controller method annotated @ResponseBody can return OrderDto as JSON. @RestController simply applies response-body behavior to all its handler methods. This makes it convenient for API-focused classes but does not create a different Spring web stack.
Spring MVC vs Spring REST — View-Based vs API-Based
Direct Answer
'Spring REST' is not a separate framework from Spring MVC. Spring MVC can render server-side views or write response bodies for HTTP APIs. @Controller and @RestController are common choices, but either style can return ResponseEntity and a @Controller method can add @ResponseBody. The real difference is return-value handling and the endpoint contract, not an absolute controller annotation split.
Code Example
// Spring MVC - returns view name, renders HTML
@Controller
public class UserMvcController {
@GetMapping("/users/{id}")
public String getUser(@PathVariable Long id, Model model) {
var user = userService.findById(id);
model.addAttribute("user", user);
return "user-profile"; // Thymeleaf renders user-profile.html
}
}
// Spring REST - returns object, serialized to JSON
@RestController // Same as @Controller + @ResponseBody on all methods
@RequestMapping("/api/users")
public class UserRestController {
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id); // Returns JSON: {"id":1,"name":"Alice"}
}
}
// You can mix both in one controller with @ResponseBody
@Controller
public class MixedController {
@GetMapping("/page")
public String htmlPage() { return "view"; } // HTML
@GetMapping("/data")
@ResponseBody // Makes this method return JSON instead of view
public User jsonData() { return new User(1L, "Bob"); }
}
Common Mistakes & Pitfalls
- Thinking they're separate frameworks: Freshers believe Spring REST is a different module or framework from Spring MVC. They're the same — @RestController is just syntactic sugar for @Controller with @ResponseBody on every method. Both use DispatcherServlet.
- Returning view names from @RestController: Writing
return "user-list";in a RestController method and expecting JSON. RestController treats String returns as literal response body, not view names. You'll get the string "user-list" as plain text, not JSON. - Mixing concerns: Adding Model parameters to RestController methods or returning ResponseEntity from @Controller methods without @ResponseBody. If you need JSON, use @RestController. If you need views, use @Controller. Don't mix unless you explicitly use @ResponseBody on specific methods.
Check your understanding
Answer these without looking back at the article.
- Can you have both @Controller and @RestController in the same Spring Boot application?
- What is spring-boot-starter-web and what does it include?
- What is the role of HttpMessageConverter in Spring REST?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.