Annotations handle HTTP requests
In Spring MVC, annotations are used to handle different types of HTTP requests (e.g., GET, POST, PUT, DELETE). These annotations map HTTP requests to specific handler methods in a controller class, allowing the application to respond appropriately to different types of requests.
Purpose:
To simplify the mapping of HTTP requests to controller methods, making it easier to build RESTful web services and manage different request types.
Common Annotations:
- `@GetMapping`: Handles HTTP GET requests.
- `@PostMapping`: Handles HTTP POST requests.
- `@PutMapping`: Handles HTTP PUT requests.
- `@DeleteMapping`: Handles HTTP DELETE requests.
- `@RequestMapping`: Can handle multiple types of HTTP requests by specifying the method type.
Table of Contents
Examples
1. `@GetMapping`
Explanation:
Purpose: Used to handle HTTP GET requests. Typically used to retrieve data from the server.
Example:
UserController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/all")
public String getAllUsers() {
return "List of all users";
}
}
```
In this example:
@GetMapping("/all")
: Maps HTTP GET requests to/users/all
to thegetAllUsers
method.
2. @PostMapping
Explanation:
- Purpose: Used to handle HTTP POST requests. Typically used to submit data to be processed by the server.
Example:
UserController.java
```java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PostMapping("/add")
public String addUser(@RequestBody User user) {
// Process the user data
return "User added successfully";
}
}
```
In this example:
@PostMapping("/add")
: Maps HTTP POST requests to/add
to theaddUser
method, which takes aUser
object from the request body.
3. @PutMapping
Explanation:
- Purpose: Used to handle HTTP PUT requests. Typically used to update existing data on the server.
UserController.java
```java
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
@PutMapping("/{id}")
public String updateUser(@PathVariable Long id, @RequestBody User user) {
// Update user with the given id
return "User with id " + id + " updated successfully";
}
}
```
In this example:
@PutMapping("/{id}")
: Maps HTTP PUT requests to/users/{id}
to theupdateUser
method, which updates the user data based on the providedid
.
4. @DeleteMapping
Explanation:
- Purpose: Used to handle HTTP DELETE requests. Typically used to delete data from the server.
Example:
UserController.java
```java
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
// Delete user with the given id
return "User with id " + id + " deleted successfully";
}
}
```
In this example:
@DeleteMapping("/{id}")
: Maps HTTP DELETE requests to/users/{id}
to thedeleteUser
method, which deletes the user with the specifiedid
.
5. `@RequestMapping`
Explanation:
Purpose: Can be used to handle multiple types of HTTP requests by specifying the method type. It is more flexible but less specific than the method-specific annotations (`@GetMapping`, `@PostMapping`, etc.).
Example:
UserController.java
```java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class UserController {
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String getUserInfo() {
return "User information";
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createUser() {
return "User created";
}
}
```
In this example:
@RequestMapping(value = "/info", method = RequestMethod.GET)
: Maps HTTP GET requests to/users/info
to thegetUserInfo
method.@RequestMapping(value = "/create", method = RequestMethod.POST)
: Maps HTTP POST requests to/users/create
to thecreateUser
method.
Summary
@GetMapping
: Handles HTTP GET requests.@PostMapping
: Handles HTTP POST requests.@PutMapping
: Handles HTTP PUT requests.@DeleteMapping
: Handles HTTP DELETE requests.@RequestMapping
: Handles multiple request types and offers flexibility.