What is the difference between @Controller and @RestController?

@Controller and @RestController are Spring MVC annotations used to handle incoming HTTP requests. The primary difference is that @Controller is typically used for web applications that return views (HTML/JSP pages), while @RestController is designed for REST APIs and returns data directly in formats such as JSON or XML.

Key Points

How @Controller Works

When a method returns a String, Spring treats it as a view name and forwards the request to a view resolver.

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home() {

        return "home";
    }
}

Flow

Client Request

|

@Controller

|

Returns View Name

|

View Resolver

|

home.jsp / home.html

Output

A web page is rendered and sent to the browser.

How @RestController Works

@RestController automatically adds @ResponseBody behavior to every method.

@RestController
public class EmployeeController {

    @GetMapping("/employee")
    public Employee getEmployee() {

        return new Employee(
                101,
                "John");
    }
}

Flow

Client Request

|

@RestController

|

Java Object

|

JSON Conversion

|

JSON Response

Output

{

"id": 101,

name": "John

}

Internal Difference

@Controller

Equivalent To

@Controller

Methods return

@RestController

Equivalent To

@Controller
@ResponseBody

Methods return

Comparison

@Controller

Purpose

Response

Requires @ResponseBody

Common Usage

@RestController

Purpose

Response

Requires @ResponseBody

Common Usage

Returning JSON from @Controller

If JSON is required in a @Controller, use @ResponseBody.

@Controller
public class EmployeeController {

    @GetMapping("/employee")
    @ResponseBody
    public Employee getEmployee() {

        return new Employee(
                101,
                "John");
    }
}

This behaves similarly to @RestController for that specific method.

Example

Online Banking Application

Web Portal

@Controller

Used for

REST API

@RestController

Used for

Benefits of @RestController

When to Use Which?

Use @Controller when

Use @RestController when

Interview Tip

A concise interview answer is

@Controller is used in Spring MVC applications to return views such as JSP or Thymeleaf pages. @RestController is a specialized version of @Controller that combines @Controller and @ResponseBody, causing all methods to return data directly as JSON or XML. Therefore, @Controller is typically used for web pages, while @RestController is preferred for REST APIs and microservices.

Last updated: 11 November 2026

Previous question: What does the @RestController annotation do in Spring Boot?

Next question: What is the difference between @RequestMapping and @GetMapping?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions