WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Intermediate · question 31 of 100

Can you discuss the difference between @Controller and @RestController annotations in Spring Boot?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

In a Spring Boot application, the @Controller and @RestController annotations are used to create web controllers that handle incoming HTTP requests. While both annotations are used to create web controllers, there are some differences between them.

The @Controller annotation is used to create a standard Spring MVC controller that can handle HTTP requests and return a view to be rendered by the client. The controller methods typically return a ModelAndView object, which contains the data to be rendered and the name of the view to be used.

    @Controller
    public class UserController {
        @Autowired
        private UserService userService;
        
        @GetMapping("/users")
        public ModelAndView getUsers() {
            List<User> users = userService.getAllUsers();
            ModelAndView modelAndView = new ModelAndView("users");
            modelAndView.addObject("users", users);
            return modelAndView;
        }
    }

In this example, we have a UserController class that is annotated with @Controller. The getUsers method handles HTTP GET requests to the /users endpoint and returns a ModelAndView object containing the list of users and the name of the view to be rendered.

The @RestController annotation is used to create a web controller that returns data in a specific format, typically JSON or XML. The controller methods typically return the data directly, rather than a ModelAndView object.

    @RestController
    public class UserRestController {
        @Autowired
        private UserService userService;
        
        @GetMapping("/users")
        public List<User> getUsers() {
            return userService.getAllUsers();
        }
    }

In this example, we have a UserRestController class that is annotated with @RestController. The getUsers method handles HTTP GET requests to the /users endpoint and returns a list of users directly.

Overall, the main difference between @Controller and @RestController is that @Controller is used to handle requests and return views, while @RestController is used to handle requests and return data directly. @RestController is a convenience annotation that combines @Controller and @ResponseBody, allowing developers to create RESTful web services more easily.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics