@GetMapping注解的理解

Spring的复杂性不是来自于它处理的对象,而是来自于自身,不断演进发展的Spring会带来时间维度上复杂性,比如SpringMVC以前版本的*@RequestMapping*,到了新版本被下面新注释替代,相当于增加的选项:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型,即*@GetMapping用于处理请求方法的GET类型,@ PostMapping用于处理请求方法的POST*类型等。

如果我们想使用传统的*@RequestMapping*注释实现URL处理程序,那么它应该是这样的:

@RequestMapping(value = “/get/{id}”, method = RequestMethod.GET)

新方法可以简化为:

@GetMapping("/get/{id}")

如何工作

所有上述注释都已在内部注释了*@RequestMapping以及方法*元素中的相应值。

例如,如果我们查看*@GetMapping注释的源代码,我们可以看到它已经通过以下方式使用RequestMethod.GET*进行了注释:

@Target({ java.lang.annotation.ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = { RequestMethod.GET })
public @interface GetMapping {
   // abstract codes
}

所有其他注释都以相同的方式创建,即*@PostMapping使用RequestMethod.POST进行注释,@ PutMapping使用RequestMethod.PUT进行*注释等。

代码示例

BuyerProductController

/**
 * 买家商品
 * Created by 李柏霖
 * 2020/10/17 20:11
 */

package com.lbl.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @GetMapping("/list")
    public void list(){

    }
}
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值