其他分享
首页 > 其他分享> > 什么时候用@PostMapping和@GetMapping和@RequestMapping

什么时候用@PostMapping和@GetMapping和@RequestMapping

作者:互联网

摘:Spring4.3中引进了{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping},来帮助简化常用的HTTP方法的映射,并更好地表达被注解方法的语义。

=================================

@GetMapping
用于将HTTP GET请求映射到特定处理程序方法的注释。具体来说,@GetMapping是一个作为快捷方式的组合注释
@RequestMapping(method = RequestMethod.GET)。

无参,@RequestParam 和@PathVaiable的情况下使用GetMapping:

@gettMapping("/test")
public ModelAndView test16(@RequestParam(“id”)Long id){}

@gettMapping("/test/{id}")
public ModelAndView (@PathVaiable(“name”) Long id){}

当方法中的参数为@RequestParam、@PathVaiable、无参的情况下使用@GetMapping

=============================

@PostMapping
用于将HTTP POST请求映射到特定处理程序方法的注释。具体来说,@PostMapping是一个作为快捷方式的组合注释@RequestMapping(method = RequestMethod.POST)。

如果传的参数是@RequestBody ,多参或者传对象的情况下使用@PostMapping注解:
@PostMapping("/getOrderList")
public List getList(@RequestBody List orderList) {}

=========================================

@RequestMapping:
一般情况下都是用@RequestMapping(method=RequestMethod.),因为@RequestMapping可以直接替代以上两个注解,但是以上两个注解并不能替代@RequestMapping,@RequestMapping相当于以上两个注解的父类!

类似的组合注解还有:
@PutMapping、@DeleteMapping、@PatchMapping

抄袭文献:
https://blog.csdn.net/qq_42455457/article/details/116793015?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.control&spm=1001.2101.3001.4242

标签:RequestMapping,RequestParam,PostMapping,GetMapping,注解,id
来源: https://blog.csdn.net/weixin_47104688/article/details/120178260