Controller vs RestController
In the age of SPAs, @RestController is your best friend.
The Swiggy App vs Dine-in Restaurant Analogy:
@Controller (Dine-in Restaurant): You go to the restaurant, the waiter takes the order, the kitchen cooks the food, and the waiter serves it to you on a decorated plate (HTML page). You get a "view".
@RestController (Swiggy Delivery): You order on Swiggy. You don't care about the plate or decoration. You just want a box (JSON Data) containing the food.
Modern frontend frameworks (Angular, React) don't want HTML from the backend. They want raw data (JSON) so they can decorate it themselves. @RestController does exactly this.
- @Controller: Used in traditional Spring MVC. It returns a
Stringwhich is the name of an HTML template (like Thymeleaf). To return data, you must add@ResponseBodyto every method. - @RestController: A convenience annotation. It is
@Controller+@ResponseBody. It tells Spring: "Do not look for HTML views. Convert the return value directly to JSON and send it in the HTTP response."
Spring uses Jackson library automatically to convert Java objects to JSON.
package com.devinhyderabad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.CommandLineRunner;
import org.springframework.beans.factory.annotation.Autowired;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private ProfileController profileController;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
Profile p = profileController.getUser(101);
System.out.println("User fetched: " + p.getName());
}
}
@RestController
@RequestMapping("/api/users")
class ProfileController {
@GetMapping("/{id}")
public Profile getUser(@PathVariable int id) {
return new Profile(id, "Deva", "deva@devinhyderabad.com");
}
}class Profile {
private int id;
private String name;
private String email;
public Profile(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
// Getters are required for Jackson to convert to JSON
public int getId() { return id; }
public String getName() { return name; }
public String getEmail() { return email; }
}Spring's Jackson library automatically converts the Profile object into JSON: {"id":101,"name":"Deva","email":"deva@devinhyderabad.com"}. In real apps, use Java Records or Lombok to reduce boilerplate.
Interview Question: "What is the difference between @Controller and @RestController?"
Answer: @Controller is used for returning web views (HTML). It expects methods to return a view name. @RestController combines @Controller and @ResponseBody. It converts the return value directly into HTTP response bodies (usually JSON) using Jackson, bypassing view resolution entirely.
Enterprise Note: In modern microservices and SPAs (Angular, React), you will almost exclusively use @RestController.
Key Takeaways
- ✅ @Controller returns HTML views; @RestController returns JSON data
- ✅ @RestController = @Controller + @ResponseBody
- ✅ Jackson library auto-converts Java objects to JSON
- ✅ Modern SPAs use @RestController exclusively for backend APIs
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login