HTTP request methods in Spring MVC
In Spring MVC, handling different types of incoming HTTP request methods is crucial for creating RESTful web services and handling various client requests. Spring MVC provides annotations that map HTTP requests to controller methods based on the request method (e.g., GET, POST, PUT, DELETE).
Key Annotations:
- `@GetMapping`: Maps HTTP GET requests to a specific method. Used for retrieving data.
- `@PostMapping`: Maps HTTP POST requests to a specific method. Used for creating or submitting data.
- `@PutMapping`: Maps HTTP PUT requests to a specific method. Used for updating existing data.
- `@DeleteMapping`: Maps HTTP DELETE requests to a specific method. Used for deleting data.
- `@RequestMapping`: Can be used with a method attribute to map specific HTTP request methods (GET, POST, etc.) to a method. It provides more flexibility and is the base annotation for mapping.
Table of Contents
1. Handling HTTP GET Requests
Example: Fetching Data
UserController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user/{id}")
public UserDTO getUserById(@PathVariable Long id) {
// Simulate fetching user from a database
UserDTO user = new UserDTO(id, "john_doe", "john.doe@example.com");
return user;
}
}
```
In this example:
– `@GetMapping(“/user/{id}”)`: Maps HTTP GET requests to `/user/{id}` URL to the `getUserById` method. This method retrieves user data based on the provided user ID.
2. Handling HTTP POST Requests
Example: Creating New Data
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("/user")
public String createUser(@RequestBody UserDTO userDTO) {
// Simulate saving user to a database
return "User created with username: " + userDTO.getUsername();
}
}
```
In this example:
@PostMapping("/user")
: Maps HTTP POST requests to/user
URL to thecreateUser
method. This method creates a new user based on the data sent in the request body.
3. Handling HTTP PUT Requests
Example: Updating Existing Data
UserController.java
```java
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PutMapping("/user")
public String updateUser(@RequestBody UserDTO userDTO) {
// Simulate updating user in a database
return "User updated with username: " + userDTO.getUsername();
}
}
```
In this example:
@PutMapping("/user")
: Maps HTTP PUT requests to/user
URL to theupdateUser
method. This method updates an existing user based on the data sent in the request body.
4. Handling HTTP DELETE Requests
Example: Deleting Data
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
public class UserController {
@DeleteMapping("/user/{id}")
public String deleteUser(@PathVariable Long id) {
// Simulate deleting user from a database
return "User with ID " + id + " has been deleted";
}
}
```
In this example:
@DeleteMapping("/user/{id}")
: Maps HTTP DELETE requests to/user/{id}
URL to thedeleteUser
method. This method deletes a user based on the provided user ID.
Summary of Example Files
- UserDTO.java: Represents the Data Transfer Object with user details.
- UserController.java: Handles various HTTP request methods (GET, POST, PUT, DELETE) and maps them to appropriate methods for handling user data.