Java
5/6/2026
4 min read

Mastering @RequestMapping, @RestController, and @Controller in Spring Boot

Mastering @RequestMapping, @RestController, and @Controller in Spring Boot

When building web applications using Spring Boot, three commonly used annotations are @RequestMapping, @RestController, and @Controller. These annotations define how your application handles HTTP requests and sends responses back to the client. Understanding them clearly is very important for both real-world development and interviews.

What is @Controller?

@Controller is used to mark a class as a Spring MVC controller that returns a view (HTML page) as a response.

This annotation is mainly used in traditional web applications where the server returns a frontend page.

Real-world example:

Consider a banking website. When a user logs in, the system verifies the details and then returns a dashboard page. This is handled using a controller.

Code example:

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

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String homePage() {
        return "home"; // returns home.html
    }
}

Here, /home returns a UI page named home.html.

What is @RestController?

@RestController is a specialized version of @Controller that returns data directly in JSON or XML format instead of a view."

It is widely used in REST APIs where the frontend and backend are separate.

Real-world example:

In an e-commerce mobile app, when you open the app and see products, the data comes from backend APIs in JSON format.

Code example:

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

@RestController
public class UserController {

    @RequestMapping("/user")
    public String getUser() {
        return "User data returned";
    }
}

This returns raw data instead of an HTML page.

What is @RequestMapping?

@RequestMapping is used to map HTTP requests to specific handler methods or classes.

It connects URLs with the correct method in your application.

Real-world example:

A website has multiple pages, like /login, /profile, /orders. Each URL is mapped to a different method using @RequestMapping.

Code example:

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

@RestController
@RequestMapping("/api")
public class ProductController {

    @RequestMapping("/products")
    public String getProducts() {
        return "List of products";
    }
}

The final endpoint becomes /api/products.

Additional Concepts (Important for Interviews)

1. Difference between @Controller and @RestController

@RestController = @Controller + @ResponseBody

This means @RestController automatically converts return values into JSON format.

In @Controller, if you want to return JSON, you must use @ResponseBody manually.

Example:

@Controller
public class DemoController {

    @RequestMapping("/data")
    @ResponseBody
    public String getData() {
        return "Data response";
    }
}

2. HTTP Method Specific Mapping

Instead of using @RequestMapping everywhere, Spring provides shortcuts:

  • @GetMapping → for GET requests

  • @PostMapping → for POST requests

  • @PutMapping → for UPDATE

  • @DeleteMapping → for DELETE

Example:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserApi {

    @GetMapping
    public String getUsers() {
        return "All users";
    }

    @PostMapping
    public String createUser() {
        return "User created";
    }
}

3. Path Variables and Request Parameters

These are commonly asked in interviews.

Example:

@GetMapping("/users/{id}")
public String getUserById(@PathVariable int id) {
    return "User id is " + id;
}

Key Differences Summary

  • @Controller → Returns HTML view

  • @RestController → Returns JSON/data

  • @RequestMapping → Maps URL to method

Difference between @Controller and @RestController

Basic Definition

@Controller is used to return a view (HTML page) from the server.

@RestController is used to return data (JSON or XML) directly from the server."

Main Difference

@Controller -> Used for web applications (returns UI pages)

@RestController -> Used for REST APIs (returns data)

Real-world Example

@Controller:

Used in websites like banking portals, where a dashboard page is returned after login.

@RestController:

Used in mobile or frontend applications where backend sends data in JSON format.

Code Examples

@Controller Example:

import org.springframework.stereotype.Controller;

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

@Controller

public class PageController {
@RequestMapping("/dashboard")
public String dashboard() {
    return "dashboard"; // returns dashboard.html
}

@RestController Example:

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

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

@RestController

public class ApiController {
@RequestMapping("/data")
public String getData() {
    return "This is API response";
}

Internal Working

@RestController = @Controller + @ResponseBody

This means @RestController automatically returns data, while in @Controller you need to use @ResponseBody manually.

When to Use

Use @Controller when building frontend-based applications (HTML, JSP, Thymeleaf).

Use @RestController when building backend APIs (used by mobile apps or frontend frameworks like React or Angular).

Conclusion

These annotations are the foundation of Spring Boot web development. If you understand when to use @Controller and when to use @RestController, you can design both UI-based and API-based applications easily. @RequestMapping and its variants help you organize your endpoints properly.

For interviews, always remember the definitions, differences, and real-world usage. Strong basics in these concepts will help you write better code and explain your knowledge clearly.

Enjoyed this article?

Subscribe to our newsletter for more backend engineering insights and tutorials.