在Spring 4.3以后,引入了@GetMapping、@PostMapping、@PutMapping、@DeleteMapping和@PatchMapping,一共5个注解。
1、@GetMapping用于将HTTP get请求映射到特定处理程序的方法注解
具体来说,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
2、@PostMapping用于将HTTP post请求映射到特定处理程序的方法注解
具体来说,@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
其他也类似。
注:
请求资源应该使用GET;
添加资源应该使用POST;
更新资源应该使用PUT;
删除资源应该使用DELETE.
有没有发现PATCH没有被使用,其实PATCH和PUT有点类似,当然也有不同,这个就交给大家自己去寻找原因了。
1、使用GetMapping
@GetMapping("/queryByNames") public String queryByNames(@RequestParam(value = "name" ,required = true) List<String> name) { return name.toString(); }
2、使用PostMapping
@PostMapping("/addUser") public String addUser(User user) { return user.toString(); }
3、使用PutMapping
@PutMapping("/updateUser") public String updateUser(@RequestBody User user) { return user.toString(); }
4、使用DeleteMapping
@DeleteMapping("/deleteUser") public String deleteUser(@RequestParam(value = "id", required = true)Integer id) { return id.toString(); }
原文链接:https://blog.csdn.net/shangrila_kun/article/details/89024485
文章评论