Spring Boot Interview Question

What is the difference between Controller and RestController?

Updated 2026-08-16 · Beginner friendly
Quick answer

Controller is used when methods return a view name, such as an HTML page, and you need ResponseBody on individual methods to return data. RestController is a shortcut that combines Controller and ResponseBody, so every method returns data directly, usually as JSON. Use RestController for REST APIs and Controller when serving web pages.

Key takeaways
  • Controller returns view names and is used for server rendered web pages.
  • RestController returns data directly as JSON or XML for APIs.
  • RestController is simply Controller combined with ResponseBody.

Controller

A method in a Controller returns a view name by default, which a template engine turns into HTML. To return raw data you must add ResponseBody to the method.

RestController

This is Controller plus ResponseBody applied to the whole class, so every method returns the data itself. This is what you want for JSON APIs.

@RestController   // = @Controller + @ResponseBody
class ApiController {
    @GetMapping("/user")
    User get() { return new User(); }  // returned as JSON
}
In the interview

The clean answer is RestController equals Controller plus ResponseBody, so use it for REST APIs and plain Controller for server rendered pages. Stating that equation is the crispest way to show you understand the link.

Frequently asked questions

When should you use Controller instead of RestController?

Use Controller when serving HTML views through a template engine, and RestController when building a REST API that returns data like JSON.

What does ResponseBody do?

It tells Spring to write the method's return value directly into the HTTP response body instead of treating it as a view name.

Want the full Spring Boot guide?

Read every Spring Boot concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the Spring Boot guide All Spring Boot questions