> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/spring/core-concepts/spring-annotations/commonly-used-annotations/controller-layer-web-and-rest-controllers.md).

# Controller Layer (Web & REST Controllers)

## About

Spring Boot provides a set of annotations for defining controllers in web applications, handling HTTP requests, and configuring response behaviors. These annotations are used to build **RESTful APIs** or **traditional MVC applications**.

## **1. Core Controller Annotations**

These annotations define the controller layer in a Spring Boot application.

### **`@Controller`**

* Marks a class as a Spring MVC controller.
* Used in **Spring MVC applications** that return HTML views.

```java
@Controller
public class MyController {
    @GetMapping("/home")
    public String home() {
        return "home";  // Returns a view name
    }
}
```

### **`@RestController`**

* Specialized version of `@Controller` that combines `@Controller` and `@ResponseBody`.
* Used in **RESTful APIs** where the response is JSON or XML instead of HTML.

{% hint style="success" %}
`@RestController = @Controller + @ResponseBody`
{% endhint %}

```java
@RestController
public class MyRestController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";  // Returns raw data, not a view
    }
}
```

## **2. Request Mapping Annotations**

These annotations map HTTP requests to controller methods.

### **`@RequestMapping`**

* Maps an HTTP request to a method or class.
* Supports various attributes like `value`, `method`, `produces`, `consumes`.
* Class-level `@RequestMapping` prefixes all method mappings.

**Example (Class-level and Method-level Mapping):**

```java
@RestController
@RequestMapping("/api")
public class MyApiController {
    
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<String> getUsers() {
        return List.of("Alice", "Bob");
    }
}
```

#### **Shortcuts for `@RequestMapping`**

Spring Boot provides specific annotations for each HTTP method:

| Annotation       | Purpose                      |
| ---------------- | ---------------------------- |
| `@GetMapping`    | Handles HTTP GET requests    |
| `@PostMapping`   | Handles HTTP POST requests   |
| `@PutMapping`    | Handles HTTP PUT requests    |
| `@DeleteMapping` | Handles HTTP DELETE requests |
| `@PatchMapping`  | Handles HTTP PATCH requests  |

### **`@GetMapping`**

* Maps HTTP **GET** requests.

```java
@GetMapping("/users")
public List<String> getUsers() {
    return List.of("Alice", "Bob");
}
```

### **`@PostMapping`**

* Maps HTTP **POST** requests (used for creating resources).

```java
@PostMapping("/users")
public String createUser(@RequestBody String user) {
    return "User created: " + user;
}
```

`@RequestBody` is used to extract JSON request payload.

### **`@PutMapping`**

* Maps HTTP **PUT** requests (used for updating resources).

```java
@PutMapping("/users/{id}")
public String updateUser(@PathVariable Long id, @RequestBody String user) {
    return "User " + id + " updated with: " + user;
}
```

### **`@DeleteMapping`**

* Maps HTTP **DELETE** requests.

**Example:**

```java
@DeleteMapping("/users/{id}")
public String deleteUser(@PathVariable Long id) {
    return "User " + id + " deleted";
}
```

#### **`@PatchMapping`**

* Maps HTTP **PATCH** requests (used for **partial updates**).

**Example:**

```java
@PatchMapping("/users/{id}")
public String updateUserPartially(@PathVariable Long id, @RequestBody String user) {
    return "User " + id + " partially updated with: " + user;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/core-concepts/spring-annotations/commonly-used-annotations/controller-layer-web-and-rest-controllers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
