springboot从零开始: @RequestMapping与@GetMapping区别

前言:

本系列博客记录 springboot 求学之路:
springboot 有很多请求方式,这里记录一下如题的一个疑问,

1.概说
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

从名字不难看出这些是定义路由的访问方式,get、post等,点进去查看他们的上层代码,例如 @GetMapping,在最上面有几行注释,如下

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
    method = {RequestMethod.GET}
)
public @interface GetMapping {
    @AliasFor(
        annotation = RequestMapping.class
    )
    .......

这里面有声明 ,很明显就是@RequestMapping的注解,并使用 get方法

@RequestMapping(
    method = {RequestMethod.GET}
)

到这里几乎就很明显了,上面的几个注解都是包含@RequestMapping的组合注解,简化了原来的使用方法。
下面这两个是等价的:

   @GetMapping(path = "/get")
   public String getName() {
       return "";
   }

   @RequestMapping(path = "/get", method = RequestMethod.GET)
   public String get() {
       return "";
   }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值