← Back to Questions
Spring Boot

Difference between @Controller and @RestController

Learn Difference between @Controller and @RestController with simple explanations, real-time examples, interview tips and practical use cases.

Difference Between @Controller and @RestController in Spring Boot

@Controller and @RestController are important annotations in Spring Boot.

Both handle HTTP requests, but they are used for different purposes.

  • @Controller → Returns HTML pages/views
  • @RestController → Returns JSON/XML data

Simple Definition

@Controller is mainly used for web pages, while @RestController is used for REST APIs.

What is @Controller?

@Controller is used in Spring MVC applications.

It mainly returns:

  • HTML pages
  • JSP pages
  • Thymeleaf templates
  • View names

@Controller Example

@Controller
public class HomeController {

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

        return "index";
    }
}

Here:

  • "index" is view name
  • Spring searches for index.html or index.jsp

What is @RestController?

@RestController is used to create REST APIs.

It mainly returns:

  • JSON data
  • XML data
  • API responses

@RestController Example

@RestController
public class CourseController {

    @GetMapping("/courses")
    public List getCourses() {

        return List.of(
            "Java",
            "Spring Boot"
        );
    }
}

Response:

[
  "Java",
  "Spring Boot"
]

Main Difference Table

Feature @Controller @RestController
Main Purpose Return HTML Views Return REST API Responses
Response Type View/Page JSON/XML
Used In Spring MVC REST APIs
@ResponseBody Needed? Yes No
Frontend Rendering Server-side Client-side

Internal Difference

Internally:

@RestController
=
@Controller
+
@ResponseBody

What is @ResponseBody?

@ResponseBody tells Spring:

"Return data directly instead of returning view name."

@Controller with @ResponseBody Example

@Controller
public class UserController {

    @ResponseBody
    @GetMapping("/users")
    public List getUsers() {

        return List.of("Naresh", "Rahul");
    }
}

This behaves similar to:

@RestController

Real-Time Example

Suppose we are building Dhanish Empower.


Using @Controller

Used for:

  • Homepage
  • Login page
  • Course HTML pages
@Controller
public class HomeController {

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

        return "index";
    }
}

Using @RestController

Used for:

  • REST APIs
  • Mobile backend APIs
  • Microservices
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @GetMapping
    public List getCourses() {

        return courseService.getCourses();
    }
}

How @Controller Works

Client Request
        ↓
@Controller
        ↓
Returns View Name
        ↓
View Resolver
        ↓
HTML Page Returned

How @RestController Works

Client Request
        ↓
@RestController
        ↓
Java Object Returned
        ↓
Jackson Converts to JSON
        ↓
JSON Response Sent

JSON Conversion in @RestController

Spring Boot internally uses:

Jackson Library

to convert Java objects into JSON.


REST API Example with JSON

@RestController
public class UserController {

    @GetMapping("/user")
    public User getUser() {

        return new User(
            1,
            "Naresh"
        );
    }
}

JSON Response:

{
  "id": 1,
  "name": "Naresh"
}

When to Use @Controller?

  • Spring MVC applications
  • HTML rendering
  • Server-side rendering
  • JSP/Thymeleaf pages

When to Use @RestController?

  • REST APIs
  • Microservices
  • Mobile backends
  • Frontend-backend separation
  • Cloud-native applications

Advantages of @RestController

  • Automatic JSON conversion
  • Cleaner REST API development
  • Reduced boilerplate code
  • Better frontend integration
  • Ideal for microservices

Production-Level Importance

In modern applications:

  • @Controller is used for MVC web applications
  • @RestController is heavily used for APIs and microservices

Most enterprise systems now use:

@RestController

because frontend and backend are usually separated.


Interview Answer

@Controller is mainly used in Spring MVC applications for returning HTML views.

@RestController is used for REST APIs and returns JSON/XML responses directly.

Internally:

@RestController
=
@Controller + @ResponseBody

Short Interview Answer

@Controller returns views, while @RestController returns JSON responses.


Frequently Asked Questions

What does @RestController internally contain?

@Controller + @ResponseBody

Which annotation is used for REST APIs?

@RestController.

Which annotation is used for HTML pages?

@Controller.

Does @RestController automatically return JSON?

Yes.

Can @Controller return JSON?

Yes, using @ResponseBody.


Conclusion

@Controller and @RestController are important annotations in Spring Boot.

@Controller is mainly used for MVC web applications, while @RestController is used for REST APIs and microservices.

Understanding their differences is essential for building scalable and production-ready applications.

Why this Spring Boot question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.