宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

Spring Boot中请求类型的说明(@ResquestMapping,@GetMapping ,@PostMapping,@PutMapping,@DeleteMapping)

2021年1月1日 711点热度 0人点赞 0条评论

在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();
}

1.png

2、使用PostMapping

@PostMapping("/addUser")
public String addUser(User user) {
    return user.toString();
}

2.png

3、使用PutMapping

@PutMapping("/updateUser")
public String updateUser(@RequestBody User user) {
    return user.toString();
}

3.png

4、使用DeleteMapping

@DeleteMapping("/deleteUser")
public String deleteUser(@RequestParam(value = "id", required = true)Integer id) {
    return id.toString();
}

4.png

原文链接:https://blog.csdn.net/shangrila_kun/article/details/89024485

标签: 暂无
最后更新:2021年1月1日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2