@RequestBody
时间: 2023-10-01 14:06:52 浏览: 77
解读@RequestBody的正确使用方法
@RequestBody is a Spring annotation that can be used to indicate that a method parameter should be bound to the body of the HTTP request. In other words, it is used to extract the request body and convert it into a Java object of the specified type. This is useful when we want to accept data from the client in JSON, XML or any other format and convert it into a Java object for processing.
For example, consider the following method in a Spring MVC controller:
```
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user) {
// code to create user object
}
```
In this example, the @RequestBody annotation is used to indicate that the User object should be extracted from the HTTP request body and bound to the method parameter named "user". The Spring framework automatically converts the request body, which is typically in JSON or XML format, into a User object.
Using the @RequestBody annotation is particularly useful when building RESTful web services, where the client sends data to the server in the request body.
阅读全文