@PostMapping
时间: 2024-05-10 15:21:08 浏览: 4
@PostMapping is an annotation used in Spring Boot to map HTTP POST requests onto specific handler methods. It is used in a controller class to handle incoming POST requests and accept data sent in the request body.
Example usage:
```
@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user) {
// code to create user
return new ResponseEntity<>(user, HttpStatus.CREATED);
}
}
```
In the example above, the `@PostMapping` annotation maps HTTP POST requests to the `/api/users` endpoint to the `createUser` method. The `@RequestBody` annotation specifies that the method expects the user object to be sent in the request body, and the method returns a `ResponseEntity` object with the created user and a status code of 201 (Created).