Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springeasy

`@RestController` vs `@Controller` — what changes?

Tags
#restcontroller#controller#spring-mvc
Back to categoryPractice quiz

Answer

`@RestController` is basically `@Controller` + `@ResponseBody`, so methods return the response body (often JSON) directly. `@Controller` is typically used for server-rendered views/templates.

Advanced answer

Deep dive

`@Controller` is an MVC controller. By default, a method returning `String` is treated as a **view name** and rendered via a view resolver (Thymeleaf, JSP, etc.).

`@RestController` applies `@ResponseBody` to every handler method, meaning:

  • return values are written to the HTTP response body,
  • message converters (e.g., Jackson) serialize objects to JSON,
  • content negotiation selects the response format.

Practical patterns

  • Use `@RestController` for APIs returning DTOs.
  • Use `@Controller` for server-side rendered pages.
  • If you must mix, be explicit with `@ResponseBody` / `ResponseEntity`.

Examples

JSON:

@RestController
class UserApi {
  @GetMapping("/users/{id}")
  UserDto get(@PathVariable long id) { /* ... */ }
}

View:

@Controller
class PageController {
  @GetMapping("/login")
  String loginPage() { return "login"; }
}

Common pitfalls

Related questions

Spring
`@RestController` vs `@Controller`: what’s the difference?
#spring#mvc#controller
Spring
`@RequestParam` vs `@PathVariable` — when do you use which?
#spring-mvc#requestparam#pathvariable
  • Returning `String` from `@Controller` expecting JSON (it becomes a view name).
  • Not using `ResponseEntity` when you need status/headers.
  • Exposing persistence entities instead of explicit DTOs.