赞
踩
@GetMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.GET)
- @GetMapping("/users")
- public ResponseEntity<List<User>> getAllUsers() {
- return userRepository.findAll();
- }
@PostMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.POST)
关于@RequestBody注解的使用
- @PostMapping("/users")
- public ResponseEntity<User> createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {
- return userRespository.save(user);
- }
@PutMapping("/users/{userId}") 等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)
- @PutMapping("/users/{userId}")
- public ResponseEntity<User> updateUser(@PathVariable(value = "userId") Long userId,
- @Valid @RequestBody UserUpdateRequest userUpdateRequest) {
- ......
- }
@DeleteMapping("/users/{userId}")等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)
- @DeleteMapping("/users/{userId}")
- public ResponseEntity deleteUser(@PathVariable(value = "userId") Long userId){
- ......
- }
一般实际项目中,我们都是 PUT 不够用了之后才用 PATCH 请求去更新数据。
- @PatchMapping("/profile")
- public ResponseEntity updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest) {
- studentRepository.updateDetail(studentUpdateRequest);
- return ResponseEntity.ok().build();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。