A Beginner’s Guide to @Controller and @RestController Annotations in Spring MVC

Alexander Obregon
3 min readAug 19, 2023

--

Image Source

Introduction

One of the most powerful aspects of Spring MVC, a component of the Spring Framework that handles web applications, is its extensible annotation-based programming model. This guide provides a beginner-friendly introduction to two of the most crucial annotations in Spring MVC: @Controller and @RestController. I'll break down what these annotations do, how they are used, and when you might prefer one over the other.

Understanding the Basics of Spring MVC

Before we dive into the specifics of @Controller and @RestController, it's helpful to understand the core principles of Spring MVC (Model-View-Controller). The MVC design pattern separates the application logic into three interconnected components:

  • Model: The Model represents the application’s data and the business rules to manipulate the data.
  • View: The View is responsible for visualizing the data provided by the Model.
  • Controller: The Controller handles the user input and interacts with the Model to perform operations on the data.

In a Spring MVC application, @Controller and @RestController are annotations used to define the Controller component.

@Controller Annotation

@Controller is a class-level annotation marking a class as a Spring MVC Controller. The @Controller annotation acts as a stereotype for the Spring MVC controller, allowing the component scanning mechanism to identify it.

Here’s a simple example of how @Controller is used in a Spring MVC application:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class GreetingController {

@RequestMapping("/greeting")
public ModelAndView greeting() {
ModelAndView modelAndView = new ModelAndView("greeting");
modelAndView.addObject("message", "Hello, Spring MVC!");
return modelAndView;
}
}

In this example, GreetingController is a Spring MVC Controller class annotated with @Controller. The greeting() method handles HTTP requests to the /greeting URL, returning a ModelAndView object. The string "greeting" is the name of the view to render, and addObject("message", "Hello, Spring MVC!") adds an attribute to the model named "message".

When a request is made to the /greeting URL, the greeting() method is invoked, and the result is rendered using the "greeting" view.

@RestController Annotation

@RestController is a specialized version of @Controller. It's a convenience annotation that combines @Controller and @ResponseBody. The @ResponseBody annotation indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).

@RestController is typically used for creating RESTful APIs where the controller response is generally JSON or XML JSON data.

Here’s a similar example to the previous one, but using @RestController:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

@RequestMapping("/greeting")
public Greeting greeting() {
return new Greeting("Hello, Spring MVC!");
}
}

In this example, the GreetingController class is annotated with @RestController, making it a REST controller. The greeting() method now returns a Greeting object. When the /greeting URL is hit, the returned Greeting object is automatically converted to JSON or XML, depending on the client's Accept header.

Choosing Between @Controller and @RestController

The choice between @Controller and @RestController depends on what you need your controller to do.

  • Use @Controller when you want to prepare a model and use it with a view. This is typical in web applications where you're returning views (such as Thymeleaf or JSP pages).
  • Use @RestController when you're creating a RESTful API. The @RestController annotation is designed for services, such as mobile services or AJAX calls, where you're returning data directly to the caller.

In essence, @Controller and @RestController serve different purposes, but both play an integral role in Spring MVC. Understanding these annotations and when to use each one is essential for a Spring MVC developer.

Conclusion

Spring MVC provides a flexible and powerful framework for creating both web applications and RESTful services. By making use of @Controller and @RestController, you can design your application to serve a range of client needs, from serving dynamic web content to providing straightforward access to your data for client applications.

Whether you’re designing an interactive web application or a state-of-the-art REST API, understanding @Controller and @RestController is a critical step towards mastery of Spring MVC.

  1. Spring Framework Official Documentation
  2. Baeldung — Spring MVC
  3. Spring Guides
Spring Boot icon by Icons8

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/