If you’ve been working with Spring MVC or Spring Boot for web applications, you’ve probably come across @Controller and @RestController. Both are used to define classes that handle incoming web requests, but here are the main differences:
- @Controller methods return a view which spring then resolves into an HTML page or template to be rendered in the browser. In simple words, @Controller returns the page of a website.
- @RestController methods return the actual data which Spring automatically converts into a format like JSON or XML and sends directly to the client. Just raw data.
@Controller

This annotation is primarily used in the development of traditional web applications that render dynamic web pages. Its function also aligns with the Model-View-Controller(MVC) pattern, where the controller has the task of preparing data then selecting a view to present that data to the client.
A class annotated with @Controller is responsible to process web requests and facilitate the rendering of a web page, like HTML or a Thymeleaf template.
Whenever a method within a @Controller returns a string, Spring interprets this string as a logical name of a view that requires resolution by ViewResolver. Here’s an example:
@Controller
public class WebPageHandler {
@GetMapping("/index")
public String displayIndexPage() {
return "index";
}
@GetMapping("/pikachu")
public String displayContactPage() {
return "pikachu";
}
}Here, the displayIndexPage() and displayContactPage() methods return strings such as “index” and “contact”. Spring interprets these as logical view names, directing the framework to locate and render the corresponding view templates.
So if I were to have index.html or contact.html in my file, Spring will return these to the client.
@RestController

@RestController is simply a combination of @Controller and @ResponseBody. No more, no less.
This means that the return value of any method within a @RestController will be bound directly to the web response body. Spring will then return the Java object as a suitable data format like JSON or XML.
@RestController
public class ApiDataService {
@GetMapping("/api/status")
public String getServiceStatus() {
return "yo:gurt";
}
@GetMapping("/api/value")
public Integer getNumericValue() {
return 42;
}
}Here, the getServiceStatus() returns a string, and getNumericValue() returns an integer. Due to @RestController, Spring will automatically format these return values directly into the HTTP response body.
For example, “yo:gurt” will be sent as plain text.
42 will be sent as a JSON data.
Leave a Reply