2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
It is the coupling of @ResponseBody and @Controller.
@Controller indicates that the class is a controller, and @ResponseBody indicates that the object returned by the controller's method is used directly as the body of the HTTP response, rather than as a view.
@GetMapping("/users/{userId}")
/users/{userId}
, and the requested URL is/users/123
,So123
can be used asuserId
Parameters are passed to the controller method.@RequestMapping
or@GetMapping
、@PostMapping
Used together with other annotations.- @GetMapping("/users/{userId}")
- public User getUserById(@PathVariable("userId") int userId) { // 根据userId获取用户信息 }
?name=value
。required=false
Attributes are set to be optional, or bydefaultValue
The property provides a default value.@RequestMapping
or@GetMapping
、@PostMapping
Used together with other annotations.- @GetMapping("/search")
- public List<User> searchUsers(@RequestParam(value = "name", required = false) String name) { // 根据提供的name参数搜索用户 }
@RequestBody
Allows you to automatically convert (through appropriate converters such as Jackson or JAXB) the request body (JSON, XML, etc.) sent by the client and bind it to an object.@RequestBody
When calling HttpChannel("application/json", "application/json"); , the request sent by the client is expected to have a non-empty request body. If the request body is empty, Spring will throw an exception.- @PostMapping("/users")
- public ResponseEntity<?> addUser(@RequestBody User user) {
- // 将接收到的User对象保存到数据库
- userService.addUser(user);
- return ResponseEntity.ok().build();
- }