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 "";
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章